流eof()和good() [英] Streams eof() and good()

查看:82
本文介绍了流eof()和good()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,


以下代码生成GOOD在一个平台上和EOF在

其他。


我希望它能产生EOF因为第二次阅读将结束

流。


哪个结果是正确的?


Adrian


#include< iostream>

#include< sstream>


int main(int argc, char * argv [])

{

std :: stringstream strm(" 5 4");


int x ;


strm> x;

strm> x;

if(strm.good())

{

std :: cout<< GOOD \ n;

}

if(strm.eof())

{

std :: cout<< EOF \ n;

}


返回0;

}

All,

This code below produces "GOOD" on one platform and "EOF" on the
other.

I expect it to produce "EOF" as the second read would hit the end of
the stream.

Which result is correct?

Adrian

#include <iostream>
#include <sstream>

int main(int argc, char *argv[])
{
std::stringstream strm("5 4");

int x;

strm >x;
strm >x;
if(strm.good())
{
std::cout << "GOOD\n";
}
if(strm.eof())
{
std::cout << "EOF\n";
}

return 0;
}

推荐答案

Adrian写道:
Adrian wrote:

全部,


这个下面的代码产生GOOD在一个平台上和EOF在

其他。


我希望它能产生EOF因为第二次阅读将结束

流。


哪个结果是正确的?


Adrian


#include< iostream>

#include< sstream>


int main(int argc, char * argv [])

{

std :: stringstream strm(" 5 4");


int x ;


strm> x;

strm> x;

if(strm.good())

{

std :: cout<< GOOD \ n;

}

if(strm.eof())

{

std :: cout<< EOF \ n;

}


返回0;

}
All,

This code below produces "GOOD" on one platform and "EOF" on the
other.

I expect it to produce "EOF" as the second read would hit the end of
the stream.

Which result is correct?

Adrian

#include <iostream>
#include <sstream>

int main(int argc, char *argv[])
{
std::stringstream strm("5 4");

int x;

strm >x;
strm >x;
if(strm.good())
{
std::cout << "GOOD\n";
}
if(strm.eof())
{
std::cout << "EOF\n";
}

return 0;
}



你错了。您只能可靠地告诉EOF _ after_

您试图读取_past_流结束并失败。


HTH,

- J.

You expect wrong. You can only tell EOF reliably _after_
you have tried to read _past_ the end of stream and failed.

HTH,
- J.


4月19日上午7:40,Jacek Dziedzic

< jacek.dziedzic.nosp ... @ gmail。 comwrote:
On Apr 19, 7:40 am, Jacek Dziedzic
<jacek.dziedzic.n.o.s.p....@gmail.comwrote:

你错了。您只能可靠地告诉EOF _after_

您试图读取_past_流结束并失败。
You expect wrong. You can only tell EOF reliably _after_
you have tried to read _past_ the end of stream and failed.



但是没有strm> x尝试阅读过去的eof,因为它一直在提取

chars


Adrian

But doesnt strm >x try and read past the eof as it keep extracting
chars

Adrian


Adrian写道:
Adrian wrote:

All,


以下代码生成GOOD在一个平台上和EOF在

其他。


我希望它能产生EOF因为第二次阅读将结束

流。


哪个结果是正确的?


Adrian


#include< iostream>

#include< sstream>


int main(int argc, char * argv [])

{

std :: stringstream strm(" 5 4");


int x ;


strm> x;

strm> x;

if(strm.good())

{

std :: cout<< GOOD \ n;

}

if(strm.eof())

{

std :: cout<< EOF \ n;

}


返回0;

}
All,

This code below produces "GOOD" on one platform and "EOF" on the
other.

I expect it to produce "EOF" as the second read would hit the end of
the stream.

Which result is correct?

Adrian

#include <iostream>
#include <sstream>

int main(int argc, char *argv[])
{
std::stringstream strm("5 4");

int x;

strm >x;
strm >x;
if(strm.good())
{
std::cout << "GOOD\n";
}
if(strm.eof())
{
std::cout << "EOF\n";
}

return 0;
}



它应该是好的。你获得EOF的平台是什么?


Zeppe

It should be GOOD. What is the platform in which you are obtaining EOF?

Zeppe


这篇关于流eof()和good()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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