473,406 Members | 2,259 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,406 software developers and data experts.

Stream state in loop conditions

This was originally posted in comp.std.c++, and never received any
replies, so I'll be rude and cross-post it here because I really am
interested in the answer. ;)

I see this idiom a LOT for reading until end-of-file:

while ( cin >> var ) { /* ... */ }
while ( getline ( cin, s ) ) { /* ... */ }

Is this guaranteed to work? The standard makes no mention of operator
void* looking at eofbit at any point. The way I'm reading the standard,
it looks like if no characters are read then failbit is set, which
operator void* uses. The idiom would work under that interpretation
because if eofbit is set then no characters are read and failbit is set
shortly thereafter, causing the loop to terminate because operator
void* relies on fail().

There's also a clause about sentry that suggests both failbit and
eofbit are set if end-of-file is reached (section 27.6.1.1.2), but I'm
not quite sure that's applicable to my question.

Thanks for your time.

Jul 23 '05 #1
3 1764
Duck Dodgers wrote:
There's also a clause about sentry that suggests both failbit and
eofbit are set if end-of-file is reached (section 27.6.1.1.2), but I'm
not quite sure that's applicable to my question.

Thanks for your time.


There are technically two errors. eofbit occurs when the end of the
input data occurs, in addition the failbit occurs because your input
operation >> fails to get what was expected by it.
Jul 23 '05 #2
Duck Dodgers wrote:
There's also a clause about sentry that suggests both failbit and
eofbit are set if end-of-file is reached (section 27.6.1.1.2), but I'm
not quite sure that's applicable to my question.

Thanks for your time.


eofbit and failbit are set in your example (probably, I need to see
what the full type definitions of your variables are). However,
they don't necessarily come on together universally. Obviously
you can fail without being at the end of file when the data in the
stream is not what is expected by the input operation. Furhter
some input operations (like reading strings or unformatted reads)
can encounter the end of file without failing the input operation.
Jul 23 '05 #3
The example is assuming that var is declared as int and s is declared
as std::string. stdin would be from an interactive terminal, and the
end-of-file key sequence is entered without any other input. The idea
is that, all else being correct, if end-of-file is reached, will
failbit get set along with eofbit so that the loop terminates?

Jul 23 '05 #4

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

Similar topics

4
by: Joe Rizla | last post by:
Is there any example code I can find which shows how to Serialize an object to System.IO.Stream. The reason why is because I want the pass the stream on: The Stream is then XSLT Transformed by...
0
by: Galore | last post by:
Hello, I'm trying to make a simple socket application, something like a chat application, but when I run it, it shows the following error when the client connects to the server: "Unable to read...
16
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? ...
2
by: Reid Priedhorsky | last post by:
Hi folks, I am implementing a forking SocketServer daemon that maintains significant internal state (a graph that takes ~30s to build by fetching from a SQL database, and eventually further...
16
by: Gary Wessle | last post by:
Hi please help with this. std::fstream iofs( f.c_str(), std::ios::in|std::ios::out ); std::cout << f << '\n' << iofs.is_open() << std::endl; puts out...
70
by: hstagni | last post by:
When i read a key using getchar() inside a loop, the program stops and wait for a key to be pressed. I actually want the program to continue its execution until a key is pressed. Look at this...
33
by: john | last post by:
I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions returning bool are mentioned: template <class Ch, class Tr= char_traits<Ch class basic_ios: public ios_base { public: //...
24
by: john | last post by:
In TC++PL3, in 21.3.3, the following definition of ios_base is provided: class ios_base{ public: // ... typedef implementation_defined2 iostate; static const iostate badbit, // stream is...
7
by: Sanyi | last post by:
What is cin's streambuffer's state and content after the following (using namespace std)? string((istreambuf_iterator<char>(cin)), istreambuf_iterator<char>()) ); I am trying to use this in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.