文件读取:feof()二进制文件 [英] file reading: feof() for binary files

查看:247
本文介绍了文件读取:feof()二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读一个二进制文件。当它到达最后。它似乎是由feof()函数终止。是否因为二进制文件没有EOF字符?如果是这样的话,我该如何解决它。



目前我的代码是使用一个while循环

  while(!feof(f))

当文件到达位置5526900.它不停止。它只是不断尝试阅读,我被困在循环。

任何人都可以告诉我为什么以及如何解决它。

谢谢

解决方案

你不应该使用feof()来循环 - 而是使用返回值fread() - 循环直到它返回零。如果您考虑读取空文件,这很容易看出 - feof()在读取操作之后返回EOF状态,因此如果用作循环控制,它将一直尝试读取伪造数据。

我不知道为什么有那么多人认为feof()(和c ++流的eof()成员)能够预测下一个读取操作是否成功,但相信我,他们不能。 / p>

I am reading a binary file. and when it reaches the end. it seems it is terminated by feof() function. is it because there is no EOF character for binary files? if so how can i solve it.

currently my code is using a while loop

while (!feof(f))

when it reaches the end of file at position 5526900. it doesn't stop. it just keeps trying to read, and i am stuck at the loop.

can anyone tell me why and how to solve it.

Thanks

解决方案

You should not use feof() to loop on - instead, use the return value of fread() - loop until it returns zero. This is easy to see if you consider reading an empty file - feof() returns the EOF status AFTER a read operation, so it will always try to read bogus data if used as a loop control.

I don't know why so many people think feof() (and the eof() member of C++ streams) can predict if the next read operation will succeed, but believe me, they can't.

这篇关于文件读取:feof()二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆