cola2003 2021-12-26 19:24 采纳率: 100%
浏览 35
已结题

将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文档中的数据存入链表中。

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2021-12-26 20:20
    关注

    供参考:

    #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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月5日
  • 已采纳回答 12月28日
  • 创建了问题 12月26日

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算