470,827 Members | 1,768 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,827 developers. It's quick & easy.

delayed eof()?

Hi,
I'm probably asking something posted several times before and maybe you
feel annoyed by answering ... please excuse me nevertheless, as I need
an urgent statement on whether and why this case is conforming to "STL
standard" (read as "is conforming to intentions of STL designers") or not:
Taking this minimal example

------
#include <iostream>
#include <sstream>

using namespace std;
int main( void )
{
stringstream s;
char ch;
s.str( "Hello" );

while ( !s.eof() )
{
s >> ch;
cout << ch;
}

cout << endl;

}
------

results in single line output

Helloo

(sending 6 instead of 5 characters to cout). I tried this with MS Visual
C++ 2005 Express Edition and GNU 4.0.x with libstdc++ 4.0.x ... they
behave equivalently.
What's so wrong about using this simple algorithm to read from string
char-wise, e.g. for parsing it ...

Best Regards
and Thanks in advance,
Thomas Urban
May 12 '06 #1
5 1470
* Thomas Urban:

int main( void )
{
stringstream s;
char ch;
s.str( "Hello" );

while ( !s.eof() )
{
s >> ch;
Here you need to check for eof (or better, failure).

End-of-file is detected upon trying to read past end-of-file.
cout << ch;
}

cout << endl;

}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 12 '06 #2
Thomas Urban wrote:
Hi,
I'm probably asking something posted several times before and maybe you
feel annoyed by answering ... please excuse me nevertheless, as I need
an urgent statement on whether and why this case is conforming to "STL
standard" (read as "is conforming to intentions of STL designers") or not:
Taking this minimal example

------
#include <iostream>
#include <sstream>

using namespace std;
int main( void )
{
stringstream s;
char ch;
s.str( "Hello" );

while ( !s.eof() )
{
s >> ch;
cout << ch;
}
eof is a condition that is reached _after_ you tried to read past the end of
the file. So after you read the last character, the loop is executed once
more. Btw: If you are getting any read error, the above loop will become an
endless loop.

Try:

while (s >> ch)
{
cout << ch;
}

if (!s.eof())
{
// an error happened
}
cout << endl;

}


May 12 '06 #3
Hello Rolf,
Try:

while (s >> ch)
{
cout << ch;
}


Indeed, this way the loop becomes even simpler ... and more correct in
one. Thank you for that hint.

Best Regards,

Thomas Urban
May 12 '06 #4
Hello Alf,
Here you need to check for eof (or better, failure).

End-of-file is detected upon trying to read past end-of-file.


That's explaining any detected "misbehaviour" quite clearly ... thank
you for that tip.
Best Regards,

Thomas Urban
May 12 '06 #5
Thomas Urban wrote:
Hi,
I'm probably asking something posted several times before and maybe you
feel annoyed by answering ... please excuse me nevertheless, as I need
an urgent statement on whether and why this case is conforming to "STL
standard" (read as "is conforming to intentions of STL designers") or not:
Taking this minimal example

------
#include <iostream>
#include <sstream>

using namespace std;
int main( void )
{
stringstream s;
char ch;
s.str( "Hello" );

while ( !s.eof() )
{
s >> ch;
cout << ch;
}

cout << endl;

}
------

results in single line output

Helloo

(sending 6 instead of 5 characters to cout). I tried this with MS Visual
C++ 2005 Express Edition and GNU 4.0.x with libstdc++ 4.0.x ... they
behave equivalently.
What's so wrong about using this simple algorithm to read from string
char-wise, e.g. for parsing it ...

This is answered in the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-15.5

--
Alan Johnson
May 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Leo Breebaart | last post: by
13 posts views Thread by Brian | last post: by
1 post views Thread by Thomas Urban | last post: by
5 posts views Thread by Jim in Arizona | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.