Checking ifstream state

F

Fraser Ross

Can I do several reading operations and then check fail() or do I need to
check fail() after every reading operation. Is it possible than a read
operation can set fail() and the next not? I am reading from a character
based stream in binary mode.

Fraser.
 
K

Karl Heinz Buchegger

Fraser said:
Can I do several reading operations and then check fail() or do I need to
check fail() after every reading operation. Is it possible than a read
operation can set fail() and the next not?

No.
When a stream has gone into a fail state, it will refuse working until
you clear that fail state.
So once a read operation has failed all subsequent read operations will
also fail.
 
T

Tobias Erbsland

Can I do several reading operations and then check fail() or do I need to
check fail() after every reading operation. Is it possible than a read
operation can set fail() and the next not? I am reading from a character
based stream in binary mode.

There is a big difference between "bad()", "fail()" and "good()". Bjarne
Stroustrup explains the stream state in his book:

(back translated from the german translation :)

== snip
If good() or eof() is set, the previous input operations are a success.
If good() is set, the next input operation could by successful. If
good() isn't set, the next input operation will fail for sure. Each
input operation an a stream that isn't "good()" has no effect.
(...)
The difference between bad() and fail() is subtly. Is the state fail()
but not bad(), then it's possible that the stream is still usable and no
characters are lost. Is the stream state bad() the stream state is fully
undefined.
== endsnip

hope this will help

best regards
Tobias
 
R

Richard Herring

Fraser Ross said:
Can I do several reading operations and then check fail() or do I need to
check fail() after every reading operation. Is it possible than a read
operation can set fail() and the next not? I am reading from a character
based stream in binary mode.
Once the fail state is set, it remains set until you clear it explicitly
by calling clear(). It's not even cleared by closing the file and
opening another, which often catches the unwary.
 
C

Catalin Pitis

If I know correctly, there is also some exception support that can be
activated. In this case, you don't have to check the state after each
operation, but you can catch the exception in a try catch block...

Catalin
 
F

Fraser Ross

"Tobias Erbsland"
If good() or eof() is set, the previous input operations are a success.

Not necessarily. When extraction is done character-wise eof will be set
when an attempt is made to read past the last character and fail will be set
in conjunction.

Fraser.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ifstream 1
multi-thread and ifstream 1
std::ifstream multithread 5
how to avoid ifstream::get() when EOF is coming 2
ifstream::getline() synatx 18
ifstream 5
ifstream seeking 3
ifstream errors 15

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top