请问这段 fscanf 的代码为什么没有达到预期的效果? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
abucus
V2EX    C

请问这段 fscanf 的代码为什么没有达到预期的效果?

  •  
  •   abucus 2020 年 10 月 11 日 2909 次点击
    这是一个创建于 2013 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我是个 C 语言初学者,查了很多网上的资料(包括 API 文档)还是没有头绪,请了解的大佬帮忙看看。题目是用 c 代码读取一个 txt 文件,这个 txt 文件里存储了图的信息,包括节点和边,内容如下

    3 a b c a b a c b c 

    第一行的数字表示这个图有多少节点。我最初始的版本读取边信息用了 while 循环

    #include<stdio.h> int main(int argc, char *argv[]) { int n; FILE *file = fopen(argv[1], "r"); fscanf(file, "%d", &n); // read node char *name; for(int i = 0; i < n; i++) { fscanf(file, "%s", name); printf("read node %s\n", name); } //read edge char *name1; char *name2; while(fscanf(file, "%s %s", name1, name2) != EOF) { printf("read edge %s %s\n", name1, name2); } } 

    编译无错误,执行的时候发现这个 while 循环不结束,而且输出的数据也很奇怪,如下

    read node a read node b read node c read edge (null) ??1?I??^H??H???PTL?F read edge (null) ??1?I??^H??H???PTL?F read edge (null) ??1?I??^H??H???PTL?F read edge (null) ??1?I??^H??H???PTL?F read edge (null) ??1?I??^H??H???PTL?F ... 

    请问这是为什么呢?

    PS 我尝试只保留 while 读取,并且在 txt 文件里只保存边信息,发现读取结果是下面这样

    read edge a (null) read edge b (null) read edge a (null) read edge c (null) read edge b (null) read edge c (null) 

    从这里看好像是 fscanf 好像不支持 “%s %s”这样的 format,于是我又有了新问题:

    1. 为什么同样是 while 循环读取,这里和上面的结果这么不一样?
    2. 要怎么样改才能正常工作?
    3 条回复    2020-10-11 09:27:33 +08:00
    pursuer
        1
    pursuer  
       2020 年 10 月 11 日 via Android
    看起来没有给 name 分配内存,像 char *name=(char *)malloc(50)或者 char name[50]这样?
    BrettD
        2
    BrettD  
       2020 年 10 月 11 日 via iPhone
    name 是野指针啊!
    abucus
        3
    abucus  
    OP
       2020 年 10 月 11 日
    @pursuer @BrettD 还真是这个问题,改了以后就正常了,多谢大佬们
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1084 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 38ms UTC 23:00 PVG 07:00 LAX 16:00 JFK 19:00
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86