文件读取:feof() 用于二进制文件
我正在读取一个二进制文件。当它到达终点时。看来它是由 feof() 函数终止的。是因为二进制文件没有EOF字符吗?如果是这样我该如何解决它。
使用 while 循环
while (!feof(f))
目前我的代码在到达文件末尾位置 5526900 时 。它不会停止。它只是不断尝试阅读,而我陷入了循环。
谁能告诉我为什么以及如何解决它。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该使用 feof() 进行循环 - 相反,使用 fread() 的返回值 - 循环直到它返回零。如果您考虑读取空文件,这一点很容易看出 - feof() 在读取操作后返回 EOF 状态,因此如果用作循环控制,它将始终尝试读取虚假数据。
我不知道为什么这么多人认为 feof() (以及 C++ 流的 eof() 成员)可以预测下一次读取操作是否会成功,但相信我,他们不能。
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.