struct node
{
char item[200];
struct node* next;
};
FILE* unionMethod(char f1name[50], char f2name[50])
{
FILE* f1;
FILE* f2;
f1=fopen(f1name,"r");
f2=fopen(f2name,"r");
char line[200];
struct node *head1=NULL;
struct node *head2=NULL;
while(!feof(f1))
{
fgets(line, 200, f1);
struct node *cur=head1->next;
if(head1==NULL)
{
head1 = (struct node *) malloc( sizeof(struct node));
fgets(line,200,f1);
strcpy(head1->item, line);
head1->next=NULL;
}
else
{
cur = (struct node *) malloc( sizeof(struct node));
fgets(line,200,f1);
strcpy(cur->item, line);
cur->next=NULL;
}
}
fclose(f1);
}
int main()
{
unionMethod("inputfile1.txt","inputfile2.txt");
return 0;
}我基本上要做的是从文件中获取行,并将它们放入一个链表中。然而,由于"feof“功能,当执行到达而零件程序停止时。我不能理解这个问题。
谢谢你的帮助。
发布于 2015-02-09 00:59:50
这是一个非常频繁的question,只需更改
while (!feof(f1))至
while (fgets(line, 200, f1))原因是,EOF标记是在fgets()尝试读取文件末尾之后设置的,因此您需要额外迭代一次才能设置它。
https://stackoverflow.com/questions/28396687
复制相似问题