Click to See Complete Forum and Search --> : Question about the state of a stream.


George2
April 17th, 2003, 10:27 PM
Hi, everyone!

Suppose I have a istream and I want to read an
integer from a istream. If the length is 0 and
I think the stream is correpted and I won't like
any other operations on the stream later. So I
think I can set the state of the stream. The following
is my sample code. I want to know whether my solution
is correct? Are there some other things to consider?



Source codes:
--------

int data;

/** return value is used to indicate whether the return
value is correct.
*/
bool A::Read (std::istream& is)
{
is.exceptions(ios::failbit|ios::eofbit);
try {
is.read ((char*)&data, sizeof (data));

if (!data) {
is.setstate (ios::failbit);
return false;
}
} catch (ios::failure e)
{
return false;
}

return true;
}
--------


Thanks in advance,
George