我需要帮助找出如何正确扫描文本文件以将信息放入链接列表。该文件是:
12 JackSprat 2 1 65000
13 HumptyDumpty 5 3 30000
17 BoPeep 2 3 30000
20 BoyBlue 3 2 58000
0
0指示文件何时结束。每当我运行我的代码时,它会循环5次,但只会保存第一行。当我打印链表的信息时,它只打印第一行5次。我需要阅读整个文件然后停在0但我很难过。这是我的代码。
struct employeeData* initializeList(struct employeeData *head, FILE *ifp ){
struct employeeData *temptr;
struct employeeData *newnode;
printf("testin\n");
while(!feof(ifp))
{
newnode = (struct employeeData*)malloc(sizeof(struct employeeData));
if (head == NULL)
{
head = newnode;
head->next = NULL;
}
else
{
temptr = head;
while(temptr->next != NULL)
{
temptr = temptr->next;
}
temptr->next = newnode;
}
fscanf(ifp, "%d%s%d%d%lf", &newnode->EMP_ID, &newnode->name, &newnode->dept, &newnode->rank, &newnode->salary);
}
return head;}
如果我删除printf(" testin \ n"),真的很奇怪;然后代码在cmd提示符下崩溃。
答案 0 :(得分:1)
newnode =(struct employeeData *)malloc(sizeof(struct employeeData)); 添加字符串:newnode-> next = NULL;
或使用calloc
如果head!= NULL则返回未更改的头部,这正是您想要的?