【发布时间】:2014-08-28 14:36:21
【问题描述】:
大家好, 从我的教授网站空间得到这个练习。我有这种文本文件:
Simon Phillips 30
Neil Peart 45
Vinnie Colaiuta 50
我想将它存储到一个列表中,所以这是我的代码:
struct listplot {
char name[25];
char sur[25];
int age;
struct listplot *next;
};
typedef struct listplot EL;
EL *list;
int filescan(EL *current)
{
FILE *in;
int count=0;
in=fopen("persone.txt", "r");
if (ferror(in))
{
printf("File Error\n");
return count;
}
do
{
current=malloc(sizeof(EL));
fscanf(in,"%s%s%d", current->name, current->sur, ¤t->age);
current->next=NULL;
if (!feof(in)) count=count+filescan(current->next);
}
while (!feof(in));
return count;
}
在main 我有:
int count=filescan(list);
此代码不起作用。 在调试中,“do”循环似乎无限,但最终程序因分段错误而崩溃。 有人可以帮我吗? 非常感谢。
【问题讨论】:
-
在没有给出最大长度的情况下不要读取字符串。并且不要假设读取成功。
-
不要递归你的文件扫描函数!
-
我为什么不能递归?为什么它不应该成功?
-
你用你的递归无限次打开文件,总是读取第一行,还有其他错误......
-
你递归打开文件,你的调用堆栈在不必要的时候会增加。
标签: c linked-list segmentation-fault codeblocks