【发布时间】:2016-11-06 12:51:09
【问题描述】:
#include<stdio.h>
struct Node{
char *name;
int time;
int sec;
int x;
int y;
struct Node *next;
};
int main(){
FILE *file;
int index;
struct Node *data=malloc(sizeof(struct Node));
struct Node *tmp=data,*tmp2=data;
int enter;
file=fopen("data.txt","r");
if(file==NULL){
printf("File was not opened!\n");
return 0;
}
while(!feof(file)){
tmp=malloc(sizeof(struct Node));
fscanf(file,"%d",&index);
fscanf(file,"%d",&tmp->time);
fscanf(file,"%d",&tmp->sec);
fscanf(file,"%d",&tmp->x);
fscanf(file,"%d",&tmp->y);
fscanf(file,"%s",tmp->name);
fscanf(file,"%'\0",&enter);
tmp->next=NULL;
tmp=tmp->next;
}
fclose(file);
while(tmp2 != NULL){
printf("file:%d\t%d\t%d\t%d\t%s\n",tmp2->timestamp,tmp2->sec,tmp2->pointx,tmp2->pointy,tmp2->name);
tmp2=tmp2->next;
}
return 0;
}
需要一些帮助来从文件中读取数据并将它们写入链接列表,然后将链接列表打印到屏幕上。但是在我启动程序后它立即停止工作。文件中的数据是这样的:
- 1 28000 41 29 50 bbb
- 2 29000 91 19 60 立方厘米
- 3 30000 23 77 92 ddd
- 4 30000 37 62 65 eee
- 5 31000 14 45 48 fff
(它们之间有标签)
我读了很多问题,但他们的答案对我没有帮助。我想我在某处遗漏了一点,但我看不到问题。是直接将数据读取到链接列表还是其他什么?感谢帮助。 //我正在查看我的代码再次感谢您的帮助**(已编辑)
【问题讨论】:
-
这个
"%'\0"和这个"%'"一模一样,按照你的说法应该怎么做?如果您阅读文档,您认为它会做什么? -
".. 它给出了一条错误消息.." 请永远不要让我们猜测您遇到的具体错误。
-
顺便说一句,你还在学习,你应该阅读很多并进行实验。请注意,您需要几天的全职工作来完成这项作业。
-
您的代码中存在编译错误。例如:代码试图访问不存在的结构中的pointx和pointy。我同意其他人将此问题作为学习机会并对其进行更多调试。通过 gdb 运行它肯定会很有趣..
标签: c linked-list text-files scanf