Node:Errors, Next:, Previous:Wrapping around iostream, Up:Usage



Errors

When doing stream I/O, some unpredictable, erroneus situations can occur at runtime. Binary streams are no exception to this phenomenon. To provide some sort of protection against this, all libbinio stream classes inherit an error reporting method called error().

It is a good idea to check the error status of a stream once in a while to see if everything is still in place. error() returns a variable of type Error, which is an integer that holds a bit field of error values for that stream. Refer to Reference for information about the possible values of this variable and their meaning.

A status of no error is always reported with a value of 0, so you can easily check if everything is still okay in a simple if construct, like this:

     if(mystream.error())
       // an error occured, do something to cure it...
     else
       // everything is still okay!
     

Two convenience error reporting methods are also included:

eof() only checks for the Eof error status, indicating whether the end of the stream has just been passed. It returns a boolean value, indicating the past the end of the stream.

ateof() is like eof() but returns true already when the stream pointer is at the last byte of the stream, not past it. This is useful when you want to read an entire file into memory in a while(!ateof()) loop. This method is only defined on streams that support reading. On write-only streams, it is not defined and not useful.

Whenever you call error(), the internal error variable is reset to the NoError state, indicating no error at all, and a subsequent call to any of the error reporting methods will return NoError again. eof() and ateof() do not reset the internal error variable. The last error status of the stream is retained.