Checking the State of a Stream

Checking the State of a Stream

The iostream family of classes provides the following member functions and operators for checking the stream’s state:

 bool good() // returns true if no error flag is setbool eof() // returns true if eofbit is setbool fail() // returns true of failbit or badbit is setbool bad()  // returns true if badbit is setbool operator!() // as fail()operator void*() // returns null if fail() is true

In other words, good() takes all flags into account, including the eofbit, whereas fail(), operator!() and operator void*() ignore eofbit. Thus, if you wish to check whether the stream is in a state of error, use fail(), operator!() or operator void*(). On the other hand, to check a failure of input operations, call the member function good().


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist