将文本文件扫描到链接列表中 [英] Scanning in a text file into a linked list
问题描述
我只是在学习链表,我必须做一个包含很多部分的作业,但我还是从头开始,我要做的第一件事就是将输入文件读入链表.该文件的一部分是:
乔治·华盛顿(George Washington),2345678约翰·亚当斯(John Adams),3456789托马斯·杰斐逊(Thomas Jefferson),4567890詹姆斯·麦迪逊(0987654)詹姆斯·梦露,9876543约翰·昆西·亚当斯(John Quincy Adams),8765432
总共包含26行.
我现在要做的只是在文件中读取.我尝试使用此代码(目前主要使用)
#include< stdio.h>#include< stdlib.h>结构节点{字符名称[20];int id;struct节点* next;}*头;int main(void){结构节点* temp;temp =(结构节点*)malloc(sizeof(结构节点));头=温度;文件* ifp;ifp = fopen("AssignmentOneInput.txt","r");int c = 0;而(c< 26){fscanf(ifp,%s",& temp->名称);fscanf(ifp,%d",& temp-> id);printf(%d \ n",c);temp = temp-> next;c ++;}
对于输出,我知道已扫描了名字和第一个ID,因为c的值显示为0(现在我随意使用c的值来控制fscanf).但是在那之后,程序崩溃了.因此问题必须出在 temp = temp-> next;
上.
我对链表非常陌生,所以我真的不知道自己在做什么.
感谢您的帮助!
首先,由于您是用 C
编写的,因此无需强制转换 malloc
.
第二,您必须自己为每个新节点分配内存.
第三,数组的名称已经衰减为指针,因此您不应该使用它的&
,因为那样的话,您将得到一个指向您不想要的指针的指针.
最后,您需要修复 scanf
语法以处理字段中的空格.
#include< stdio.h>#include< stdlib.h>#include< string.h>结构节点{字符名称[20];int id;struct节点* next;}*头;int main(void){结构节点* temp;temp = malloc(sizeof(结构节点));temp-> next = NULL;头=温度;文件* ifp;ifp = fopen("AssignmentOneInput.txt","r");int c = 0;字符缓冲区[1024];memset(缓冲区,0,1024);而(c< 5){fgets(缓冲区,1024,ifp);sscanf(缓冲区,%19 [^,],%d",临时名称,& temp-> id);printf(%d%s%d \ n",c,temp->名称,temp-> id);temp-> next = malloc(sizeof(结构节点));temp = temp-> next;temp-> next = NULL;c ++;}}
I'm just learning about linked lists and I have to do an assignment that has many parts, but I'm starting out and the very first thing I need to do is read in an input file into a linked list. Part of the file is:
George Washington, 2345678
John Adams, 3456789
Thomas Jefferson, 4567890
James Madison, 0987654
James Monroe, 9876543
John Quincy Adams, 8765432
and contains a total of 26 lines.
All I want to do now is simply read in the file. I try by using this code (in main for now)
#include <stdio.h>
#include <stdlib.h>
struct node{
char name[20];
int id;
struct node *next;
}*head;
int main(void){
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
head = temp;
FILE *ifp;
ifp = fopen("AssignmentOneInput.txt", "r");
int c = 0;
while(c<26){
fscanf(ifp, "%s", &temp->name);
fscanf(ifp, "%d", &temp->id);
printf("%d\n", c);
temp = temp->next;
c++;
}
For the output, I know that the first name and the first ID are scanned in, because the value of c is displayed as 0 (right now I'm arbitrarily using the value of c to control the fscanf). But after that, the program crashes. So the problem must be with temp = temp->next;
It compiles fine.
I am very new to linked lists, so I really don't know what I'm doing.
Your help is appreciated!
First of all, since you are writing in C
, there's no need to cast the malloc
.
Second, you must allocate memory for each new node yourself.
Third, the name of an array already decays to a pointer, so you should not take &
of it because then you'll get a pointer to a pointer which is not what you want.
Finally,you need to fix your scanf
syntax to handle spaces in your fields.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
char name[20];
int id;
struct node *next;
}*head;
int main(void){
struct node *temp;
temp = malloc(sizeof(struct node));
temp->next = NULL;
head = temp;
FILE *ifp;
ifp = fopen("AssignmentOneInput.txt", "r");
int c = 0;
char buffer[1024];
memset(buffer, 0, 1024);
while(c<5){
fgets(buffer, 1024, ifp);
sscanf(buffer, "%19[^,], %d", temp->name, &temp->id);
printf("%d %s %d\n",c, temp->name, temp->id);
temp->next = malloc(sizeof(struct node));
temp = temp->next;
temp->next = NULL;
c++;
}
}
这篇关于将文本文件扫描到链接列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!