问题描述
我刚做了一个循环
ifstream i(myfile,ios :: binary);
while(!ieof( ))
{
i.read(buff,2);
}
嗯它应该已经退出循环但是它试图再次进行阅读
操作......
现在......我知道我应该检查一下它的返回阅读,但阅读
返回一个ifstream ...
我如何检查数据是否实际读取以及什么
哎呀eof有问题吗?
阅读精美的手册...看来,只有在你尝试阅读并失败的
之后,eof才会成立......
原谅我但是有点傻吗?
Huh ?
那么? ''ifstream''有''good()''成员,并且已经转换为''void *'',
所以你可以做
而(i.read(buff,2))
或
while(i.read(buff,2).good( ))
eof没什么问题。你可能不明白
如何使用它。试试RTFM。当''读'成功和/或失败时,或者当它读取的时间比请求的要少时,要特别注意设置的标志是什么?
。然后根据你在调用''read'后看到的标志
执行适当的操作。另外,得到一本体面的书,
无格式I / O解释。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
只要你以正确的方式使用它,eof没有任何问题 -
你不是。更改您的代码以使用常见问题解答
中所述的eof并查看是否可以解决您的问题。
Gavin Deane
I just did a loop
ifstream i(myfile, ios::binary);
while (!i.eof())
{
i.read(buff, 2);
}
Well it should have come out of the loop but it tried to do the read
operation again...
Now... I know I should have checked the return of the read, but read
returns an ifstream...
How do I go about checking that the data was actually read and what
the heck is wrong with eof?
Reading the fine manual... It seems that eof will only be true after
you have tried to read and failed...
Pardon me but isn''t that a bit silly?
Huh?
So? ''ifstream'' has ''good()'' member, and it has conversion to ''void*'',
so you can do
while (i.read(buff, 2))
or
while (i.read(buff, 2).good())
Nothing the heck is wrong with eof. You probably don''t understand
how to use it. Try RTFM. Pay specific attention to what flags are
set when ''read'' succeeds and/or when it fails, or when it reads less
than requested. Then perform proper actions depending on the flags
you see set after calling ''read''. Also, get a decent book that has
unformatted I/O explained.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Nothing is wrong with eof as long as you use it in the right way -
which you aren''t. Change your code to use eof as described in the FAQ
and see if that fixes your problem.
http://www.parashift.com/c++-faq-lit....html#faq-15.4
http://www.parashift.com/c++-faq-lit....html#faq-15.5
Gavin Deane
这篇关于ifstream eof没有报告eof?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!