473,386 Members | 1,758 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,386 software developers and data experts.

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 1647
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Leo Breebaart | last post by:
Hi all, I have a question about Python and delayed evaluation. Short-circuiting of Boolean expressions implies that in: >>> if a() and b(): any possible side-effects the call to b() might...
13
by: Brian | last post by:
Hi all... This question is more for the GURUs out there. It is not a question on how to do something, but why it happens, and I am trying to figure out if there is a pattern. I am using IE, but...
0
by: subi | last post by:
Hi, I don't know where's the best place to post my question. I hope it suits this group. I have created an assembly with a delay sign attribute set to true in the AssemblyInfo.cs. And the key...
11
by: TTroy | last post by:
Hello C programmers, Can someone tell me why ungetc can't sent back EOF, but it's sister function getc has no trouble sending it to us? For a file, this might not make a difference, but for an...
3
by: Nospam | last post by:
I'm trouble shooting my obfuscated program. I'd like suggestions on some tools to help. I'm using vs2005 with delayed signing option from the project settings and reading the hash from a key.snk...
1
by: Thomas Urban | last post by:
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...
5
by: Jim in Arizona | last post by:
I realize that by design, web client/server interaction is disconnected. I'm wondering if there is a way that when a client/server interaction occurs, if there is a way to start a time on the web...
2
by: Amey Agnihotri | last post by:
Hi! I'm trying to run a few AT commands on a GSM modem. I'm doing this in C#.Net 2005. When I run a few commands one after the other, the response generation takes a long time. The response for...
8
by: Grorange | last post by:
Writing ASP.NET pages, I have a need for client-side functions to prevent going to the server for every small change. I have a list of server side created checkboxes filled with a lot of...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.