C file size discrepency

前端 未结 2 1171
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 00:30

I am trying to learn C, and am currently working on a toy script. Right now, it simply opens a text file, reads it char by char, and spits it out onto the command line.

2条回答
  •  春和景丽
    2021-01-23 01:13

        char tmp = getc(fp); //current letter in file
        int i=0;
        while (tmp != EOF) //End-Of-File (defined in stdio.h)
    

    You need to check the value returned by getc for EOF. Instead, you convert it to a char and then check whether that's equal to EOF converted to a char. But what if the value of char that converts to EOF is actually in the file? Check the docs, getc returns an int.

    You have other mistakes as well.

提交回复
热议问题