473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Streams eof() and good()

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;
}

Apr 19 '07 #1
12 2265
Adrian wrote:
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;
}
You expect wrong. You can only tell EOF reliably _after_
you have tried to read _past_ the end of stream and failed.

HTH,
- J.
Apr 19 '07 #2
On Apr 19, 7:40 am, Jacek Dziedzic
<jacek.dziedzic.n.o.s.p....@gmail.comwrote:
You expect wrong. You can only tell EOF reliably _after_
you have tried to read _past_ the end of stream and failed.
But doesnt strm >x try and read past the eof as it keep extracting
chars

Adrian

Apr 19 '07 #3
Adrian wrote:
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;
}
It should be GOOD. What is the platform in which you are obtaining EOF?

Zeppe
Apr 19 '07 #4
On Apr 19, 8:19 am, Zeppe
<zeppe.remove.all.this.long.comm...@email.itwrot e:
Adrian wrote:
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;
}

It should be GOOD. What is the platform in which you are obtaining EOF?
FreeBSD 5.4 gcc 3.4.2 Reports EOF
Unixware 7.1.1 gcc 2.95.2 Reports GOOD
XP VS2005 Reports EOF (not that I trust ms c++)
SunOs 5.6 native REPORTS EOF

Apr 19 '07 #5
Adrian wrote:
On Apr 19, 8:19 am, Zeppe
<zeppe.remove.all.this.long.comm...@email.itwrot e:
>Adrian wrote:
>>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;
}
It should be GOOD. What is the platform in which you are obtaining EOF?

FreeBSD 5.4 gcc 3.4.2 Reports EOF
Unixware 7.1.1 gcc 2.95.2 Reports GOOD
These two are old compilers. Why dont you try gcc 4.x.x and see the results?
The later versions of gcc follow c++ standards more then old versions
XP VS2005 Reports EOF (not that I trust ms c++)
SunOs 5.6 native REPORTS EOF
Not sure about these
Apr 19 '07 #6
These two are old compilers. Why dont you try gcc 4.x.x and see the results?
The later versions of gcc follow c++ standards more then old versions
Wish I could - anything greater then 2.95.2 wont compile on unixware!
XP VS2005 Reports EOF (not that I trust ms c++)
SunOs 5.6 native REPORTS EOF
Apr 19 '07 #7
On Apr 19, 10:11 am, Adrian <n...@bluedreamer.comwrote:
On Apr 19, 7:40 am, Jacek Dziedzic

<jacek.dziedzic.n.o.s.p....@gmail.comwrote:
You expect wrong. You can only tell EOF reliably _after_
you have tried to read _past_ the end of stream and failed.

But doesnt strm >x try and read past the eof as it keep extracting
chars
indeed, but you did not keep extracting characters.
So the result your getting is probably implementation defined.
In other words, good() will eventually fail, the question is "why?".
The typical use for eof() is to verify if the extraction failed for a
reason _other_ than the end marker was found.

#include <iostream>
#include <sstream>

int main()
{
std::istringstream strm("5 4 3 2 1");

int x;
while( strm.good() && (strm >x) )
{
std::cout << "x = " << x;
std::cout << std::endl;
}
if( !strm.eof() )
{
std::cout << "stream's failbit or badbit flag set\n";
} else {
std::cout << "stream's eof flag set\n";
}
}

/*
x = 5
x = 4
x = 3
x = 2
x = 1
stream's eof flag set
*/

Apr 19 '07 #8
Adrian wrote:
On Apr 19, 8:19 am, Zeppe
<zeppe.remove.all.this.long.comm...@email.itwrot e:
>It should be GOOD. What is the platform in which you are obtaining EOF?

FreeBSD 5.4 gcc 3.4.2 Reports EOF
Unixware 7.1.1 gcc 2.95.2 Reports GOOD
XP VS2005 Reports EOF (not that I trust ms c++)
SunOs 5.6 native REPORTS EOF
Dear Adrian,

my bad.

Actually it seems that the standard requires to set the eof bit if the
end of input is reached why reading a number. I got confused because I
recalled that in the Stroustrup said that if an operation succeeds
good() should return true. Now, the stroustrup is a wonderful book and
it hardly contains errors: in fact, it briefly says:

"If the state is good() the previous input operation succeeded."

which is actually true, but the opposite is not! That means that you
have to check fail() to know if the last operation failed.

That's it. Remember to check the state anyway, because for example if
you'd have taken the input from the command line there would have been a
last \n at the end of the line that would have prevented the stream to
return the eof after the last read number.

A last note: you can generally trust visual c++ 8, the new version of
the microsoft compiler is very compliant with the standard. gcc 2.95 is
very likely to be not compliant, on the other hand, being absolutely
obsolete.

Regards,

Zeppe

Apr 19 '07 #9
On Apr 19, 9:43 am, Salt_Peter <pj_h...@yahoo.comwrote:
But doesnt strm >x try and read past the eof as it keep extracting
chars

indeed, but you did not keep extracting characters.
This is what I was looking for - to see if the standard says eof
should be set or not

Your code produces "stream's eof flag set" on unixware and FreeBSD
which is in line with my tests that if you try and read at then end of
the stream eof is set (as it should be)

The reason for the question is that I have something like the
following code. Now with BSD the stream can be !good() but still have
a valid record.

Or should I change SomeClass to reset eof bit if it read a whole
record. I want to follow the way the standard implents operator>for
builtin types.

----------------------------

class SomeClass;

std::ifstream in("some file");
std::string line;

while(getline(in, line))
{
std::stringstream strm(line);

SomeClass a;
strm >a;
if(strm.good() || strm.eof())
{
// do something good
}
else
{
// report error
}
}

Apr 19 '07 #10
On Apr 19, 4:19 pm, Zeppe
<zeppe.remove.all.this.long.comm...@email.itwrot e:
Adrian wrote:
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?
Implementation defined, but in practice, I find it difficult to
believe that a correct implementation would not set eof.
#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;
}
It should be GOOD. What is the platform in which you are obtaining EOF?
According to what? I would expect EOF, but I don't think the
standard guarantees it.

The real question is what he is actually trying to do. In
practice, ios::good() is of no use what so ever, and ios::eof()
only after input has failed.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 19 '07 #11
James Kanze wrote:
>>Which result is correct?

Implementation defined, but in practice, I find it difficult to
believe that a correct implementation would not set eof.
is not, look at my other post. The standard requires eof.
>It should be GOOD. What is the platform in which you are obtaining EOF?

According to what? I would expect EOF, but I don't think the
standard guarantees it.
I would have expected good() if the last operation was successful, but I
was wrong. there is fail() for that ^^
The real question is what he is actually trying to do. In
practice, ios::good() is of no use what so ever, and ios::eof()
only after input has failed.
oh well, that one seemed a toy problem created to make a question,
didn't it?

Regards,

Zeppe
Apr 19 '07 #12
On Apr 19, 5:43 pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Apr 19, 10:11 am, Adrian <n...@bluedreamer.comwrote:
On Apr 19, 7:40 am, Jacek Dziedzic
<jacek.dziedzic.n.o.s.p....@gmail.comwrote:
You expect wrong. You can only tell EOF reliably _after_
you have tried to read _past_ the end of stream and failed.
But doesnt strm >x try and read past the eof as it keep extracting
chars
In every implementation I've seen, yes.
indeed, but you did not keep extracting characters.
But the << operator probably did. Otherwise, how did it know to
stop.
So the result your getting is probably implementation defined.
In other words, good() will eventually fail, the question is "why?".
The typical use for eof() is to verify if the extraction failed for a
reason _other_ than the end marker was found.
And it's not 100% reliable even for that.
#include <iostream>
#include <sstream>
int main()
{
std::istringstream strm("5 4 3 2 1");

int x;
while( strm.good() && (strm >x) )
You don't need the strm.good() here (and it's not very idiomatic
to use it).
{
std::cout << "x = " << x;
std::cout << std::endl;
}
if( !strm.eof() )
{
std::cout << "stream's failbit or badbit flag set\n";
} else {
std::cout << "stream's eof flag set\n";
And also failbit or badbit.
}
}
/*
x = 5
x = 4
x = 3
x = 2
x = 1
stream's eof flag set
*/
The usual idiom is:

while ( strm >x ) {
std::cout << "x = " << x << std::endl ;
}
if ( strm.bad() ) {
std::cout << "Hard read error encountered" << std::endl ;
} else if ( ! strm.eof() ) {
std::cout << "Format error encountered" << std::endl ;
} else {
std::cout << "No error (probably)" << std::endl ;
}

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 19 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ronald Legere | last post by:
The new itertools stuff is pretty cool. One thing that bothers me though is that there seems to be no way to copy an iterator. How does one work around this? Is there a trick that I am missing? ...
3
by: Scott Brady Drummonds | last post by:
Hello, all, My most recent assignment has me working on a medium- to large-sized Windows-based C++ software project. My background is entirely on UNIX systems, where it appears that most of my...
8
by: bonj | last post by:
hello I hope somebody can help me get my head around this area of 'stream' programming... I know that streams are very fashionable nowadays so hopefully there'll be lots of replies. ;-) ...
11
by: Kobu | last post by:
I have a question about C's abstract "streams" (that I can't seem to FULLY understand from reading several tutorials). Streams seems to suggest that input can be treated continously if needed....
2
by: Aaron | last post by:
I have some existing code that accepts a filename, opens that file, and then processes it line-by-line. What I want to do is modify this routine so that instead I can simply pass a huge char*...
3
by: Wilson | last post by:
hi, one aspect of a program i am creating needs to extract a certain line of text from a simple ".txt" file. However i can only find a way of extract everything present in the file using i/o...
1
by: beatTheDevil | last post by:
Hello all, I have a question that concerns how C++ input streams (istream, ifstream, istringstream, etc.) behave using the extraction (>>) operator when at the end of a stream's contents. For...
11
by: Diego Martins | last post by:
for me, these items are in the 'tricky zone' of C++ does anyone know good material with that? (dealing with subtle details, pitfalls, good practices...) anything like the Effective series from...
43
eof
by: braver | last post by:
I'd like to check, for a filehandle f, that EOF has been reached on it. What's the way to do it? I don't want to try/except on EOF, I want to check, after I read a line, that now we're in the EOF...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.