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如保存、显示...
}