C 库函数feof(FILE*) 判断文件末尾的问题(备忘)

feof(FILE*) 判断文件末尾的问题

E文:

The mechanism feof(FILE*) used to judge the end of a file: When the file pointer is at the end of a file, feof(FILE*) still returns 0 unless reads one more time to course a I/O error. Therefore, a while loop using feof(FILE*) as the judgment condition always reads one more time, and the last time of reading will fail, so buffer stayed unchanged which looked like it repeated reading once.

 

中文:

feof(FILE*) 判断文件末尾的机制:文件指针在文件末尾的时候,feof(FILE*) 依然返回 0,除非再读一次导致发生 I/O 错误。因此用 feof(FILE*) 作为判断条件的 while 循环始终会多读一次,而最后一次的读取是失败的,buffer 也就没有改变,看起来就像是重复读了一次。

 

其它文件末尾判断方法:(不用feof())

fgetc()读文件时可用:

    while((ctmp = fgetc(fp)) != EOF)            //EOF: -1

    {

        //正常操作ctmp如保存、显示...

    }

fread()读文件时可能:

    while(fread(buffer, size, num, file) == num)

    {

        //正常操作buffer如保存、显示...

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值