将txt文件中的数据存入链表中

将txt文件中的数据存入链表中

代码如下

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct name_code
{
    char ge_code_name[300];
    struct name_code*next;
};

struct name_code* hhcreat(FILE*name);
struct name_code* hhinsert(struct name_code*hh,FILE*name);
void nspnt(struct name_code *hh);

int main()
{

    struct name_code *hh=NULL;
    FILE*name;
    name=fopen("genenname.txt","r");
    hh=hhcreat(name);
     nspnt(hh);
    hh=hhinsert(hh,name);
    nspnt(hh);
    return 0;
}

struct name_code* hhcreat(FILE*name)
{
    struct name_code *hh;
    hh=(struct name_code*)malloc(sizeof(struct name_code));
    fscanf(name,"%s",&hh->ge_code_name);
    hh->next=NULL;
    return hh;
}

struct name_code* hhinsert(struct name_code*hh,FILE*name)
{
    struct name_code *node=NULL,*end=NULL;
    end=hh;
    while(!feof(name))
    {
        node=(struct name_code*)malloc(sizeof(struct name_code));
        fscanf(name,"%s",&node->ge_code_name);
        end->next=node;
        end=node;
    }
    end->next=NULL;
    return hh;
};

void nspnt(struct name_code *hh)
{
    if(hh==NULL)
    {
        printf("error!");
        return;
    }
    else
    {
        while(hh!=NULL)
        {
            printf("%s\n",hh->ge_code_name);
            hh=hh->next;
        }
    }
}


下面是一部分数据

7896736
7896738
7896740OR4F17///OR4F5///OR4F4
7896742LOC100134822///LINC00266-1
7896744OR4F29///OR4F21///OR4F16///OR4F3
7896746
7896748
7896750
7896752
7896754LOC100287934///LOC100287497
7896756FAM87A
7896759LINC01128
7896761SAMD11///NOC2L
7896779KLHL17
7896798PLEKHN1
7896817ISG15
7896822AGRN
7896859MIR200B
7896861MIR200A
7896863MIR429
7896865TTLL10
7896878B3GALT6
7896882SCNN1D
7896908PUSL1///CPSF3L
7896917CPTP
7896921TAS1R3
7896929VWA1
7896937ATAD3C///ATAD3A
7896952ATAD3A
7896961ATAD3B///ATAD3A

只能输出链表头,并且报错

我希望他能将txt文档中的数据存入链表中。

供参考:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct name_code
{
    char ge_code_name[300];
    struct name_code*next;
};
struct name_code* hhcreat(FILE*name);
struct name_code* hhinsert(struct name_code*hh,FILE*name);
void nspnt(struct name_code *hh);
int main()
{
    struct name_code *hh=NULL;
    FILE*name;
    name=fopen("genenname.txt","r");
    if(name == NULL){
       printf("File open fail!");//修改
    }
    else{                       //修改
       hh=hhcreat(name);
                                //nspnt(hh); 修改
       hh=hhinsert(hh,name);
       fclose(name);            //修改
       nspnt(hh);
    }
    system("pause");
    return 0;
}
struct name_code* hhcreat(FILE*name)
{
    struct name_code *hh;
    hh=(struct name_code*)malloc(sizeof(struct name_code));
    fscanf(name,"%s",hh->ge_code_name);  //修改
    hh->next=NULL;
    return hh;
}
struct name_code* hhinsert(struct name_code*hh,FILE*name)
{
    struct name_code *node=NULL,*end=NULL;
    end=hh;
    while(1) //(!feof(name)) 修改
    {
        node=(struct name_code*)malloc(sizeof(struct name_code));
        node->next=NULL;                                    //修改
        if(fscanf(name,"%s",node->ge_code_name)!=1) break; //修改
        end->next=node;
        end=node;
    }
    free(node);   //修改
    return hh;
};
void nspnt(struct name_code *hh)
{
    if(hh==NULL)
    {
        printf("error!");
        return;
    }
    else
    {
        while(hh!=NULL)
        {
            printf("%s\n",hh->ge_code_name);
            hh=hh->next;
        }
    }
}

img