ifstream eof没有报告eof? [英] ifstream eof not reporting eof?

查看:59
本文介绍了ifstream eof没有报告eof?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚做了一个循环


ifstream i(myfile,ios :: binary);


while(!ieof( ))

{

i.read(buff,2);

}


嗯它应该已经退出循环但是它试图再次进行阅读

操作......

现在......我知道我应该检查一下它的返回阅读,但阅读

返回一个ifstream ...

我如何检查数据是否实际读取以及什么

哎呀eof有问题吗?

解决方案

6月13日上午11点17分,SpreadTooThin< bjob​​rie ... @ gmail.comwrote :


我刚做了一个循环


ifstream i(myfile,ios :: binary);


while(!ieof())

{

i.read(buff,2);


}


好​​吧它应该已经退出循环但是它试图再次进行阅读

操作......

现在......我知道我应该检查读取的返回,但是读取

返回一个ifstream ...

如何检查数据是否实际读取以及

eof的问题是什么?



阅读精美的手册...看来,只有在你尝试阅读并失败的
之后,eof才会成立......

原谅我但是有点傻吗?


SpreadTooThin写道:


我刚做了一个循环


ifstream i(myfile,ios :: binary);


while( !即())

{

i.read(buff,2);

}


好​​吧它应该已经退出循环但是它试图再次执行阅读

操作...



Huh ?


现在......我知道我应该检查读取的返回,但是读取

返回一个ifstream ...



那么? ''ifstream''有''good()''成员,并且已经转换为''void *'',

所以你可以做


而(i.read(buff,2))





while(i.read(buff,2).good( ))


我如何检查数据是否实际读取以及什么

eof错误?



eof没什么问题。你可能不明白

如何使用它。试试RTFM。当''读'成功和/或失败时,或者当它读取的时间比请求的要少时,要特别注意设置的标志是什么?
。然后根据你在调用''read'后看到的标志

执行适当的操作。另外,得到一本体面的书,

无格式I / O解释。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


6月13日,18:17,SpreadTooThin< bjob​​rie ... @ gmail.comwrote:


我刚做了一个循环


ifstream i(myfile,ios :: binary);


while(!alsoof())

{

i.read(buff,2);


}


它应该是从循环,但它试图再次读取

操作......

现在......我知道我应该检查读取的返回,但是阅读

返回一个ifstream ...

我如何检查数据是否实际读取以及什么

eof错误?



只要你以正确的方式使用它,eof没有任何问题 -

你不是。更改您的代码以使用常见问题解答

中所述的eof并查看是否可以解决您的问题。
http://www.parashift.com/c++-faq-lit....html#faq-15.4
http:/ /www.parashift.com/c++-faq-lit....html#faq-15.5

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?

解决方案

On Jun 13, 11:17 am, SpreadTooThin <bjobrie...@gmail.comwrote:

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?


SpreadTooThin wrote:

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...

Huh?

Now... I know I should have checked the return of the read, but read
returns an ifstream...

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())

How do I go about checking that the data was actually read and what
the heck is wrong with eof?

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


On 13 Jun, 18:17, SpreadTooThin <bjobrie...@gmail.comwrote:

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?

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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