0
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”功能,当零件程序停止时执行到达。我无法理解这个问题。

谢谢你的帮助。

4

1 回答 1

0

这是一个非常常见的问题,只需更改

while (!feof(f1))

while (fgets(line, 200, f1))

原因是EOF标记是在fgets()尝试读取文件末尾之后设置的,因此您需要进行一次额外的迭代才能设置它。

于 2015-02-08T16:59:50.113 回答