cola2003 2021-12-28 19:50 采纳率: 100%
浏览 501
已结题

将文件中的数据读入链表中

将文件中的数据读入链表中

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct name_code
{
    struct name_code*pro;
    char code[7];
    char ge_code_name[600];
    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(void)
{
    struct name_code *hh=NULL;
    FILE*name;
    name=fopen("genenname.txt","r");
    hh=hhcreat(name);
    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->code);
    char a,b;
    a=getc(name);
    b=getc(name);
    fseek(name,-2L,SEEK_CUR);
    if(b!='\n')
    {
        fscanf(name,"%s",hh->ge_code_name);
    }
    hh->pro=NULL;
    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->code);
        char a,b;
        a=getc(name);
        b=getc(name);
        fseek(name,-2L,SEEK_CUR);
        if(b!='\n')
        {
            fscanf(name,"%s",hh->ge_code_name);
        }
        node->pro=end;
        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 %s\n",hh->code,hh->ge_code_name);
            hh=hh->next;
        }
    }
}


下面是文件中的一部分数据

7896736    
7896738    
7896740    OR4F17///OR4F5///OR4F4
7896742    LOC100134822///LINC00266-1
7896744    OR4F29///OR4F21///OR4F16///OR4F3
7896746    
7896748    
7896750    
7896752    
7896754    LOC100287934///LOC100287497
7896756    FAM87A
7896759    LINC01128
7896761    SAMD11///NOC2L
7896779    KLHL17
7896798    PLEKHN1
7896817    ISG15
7896822    AGRN
7896859    MIR200B
7896861    MIR200A
7896863    MIR429
7896865    TTLL10
7896878    B3GALT6
7896882    SCNN1D
7896908    PUSL1///CPSF3L
7896917    CPTP
7896921    TAS1R3
7896929    VWA1
7896937    ATAD3C///ATAD3A
7896952    ATAD3A
7896961    ATAD3B///ATAD3A
7896983    
7896985    MIB2
7897006    MMP23A///MMP23B
7897026    CALML6
7897034    GABRD
7897044    PRKCZ
7897066    FAAP20
7897068    SKI
7897076    MORN1
7897078    RER1
7897089    PLCH2
7897119    FAM213B
7897128    ACTRT2
7897132    PRDM16
7897154    ARHGEF16
7897172    TPRG1L///TPRG1L
7897179    TP73
7897196    CCDC27
7897210    DFFB
7897227    AJAP1
7897236    KCNAB2
7897257    RNF207
7897263    RNF207
7897277    LINC00337
7897280    HES3
7897286    LOC100130071
7897288    ESPN
7897295    TAS1R1
7897305    ZBTB48
7897322    PHF13
7897329    THAP3///DNAJC11
7897339    CAMTA1
7897368    

调试显示只读入了数字,而没有读入后面的字母

我的问题是不知到怎么判断数字后面是不是有字母

我希望能将文件中的数据读入链表

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-12-28 20:12
    关注

    用fscanf,%d和%s,不用管后面有没有字符,用fscanf(fp,"%d%s",...)可以逐行读入

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月29日
  • 已采纳回答 12月29日
  • 创建了问题 12月28日

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)