C++ IOS Library - good


Advertisements

Description

It is used to check whether state of stream is good.

Declaration

Following is the declaration for ios::good() function.

bool good() const;

Parameters

none

Return Value

True if none of the stream's state flags are set.

False if any of the stream's state flags are set (badbit, eofbit or failbit).

Exceptions

Strong guarantee − if an exception is thrown, there are no changes in the stream.

Data Races

Accesses the stream object.

Concurrent access to the same stream object may cause data races.

Example

In below example explains about ios::good().

#include <iostream>     
#include <sstream>      

void print_state (const std::ios& stream) {
   std::cout << " good()=" << stream.good();
   std::cout << " eof()=" << stream.eof();
   std::cout << " fail()=" << stream.fail();
   std::cout << " bad()=" << stream.bad();
}

int main () {
   std::stringstream stream;

   stream.clear (stream.goodbit);
   std::cout << "goodbit:"; print_state(stream); std::cout << '\n';

   stream.clear (stream.eofbit);
   std::cout << " eofbit:"; print_state(stream); std::cout << '\n';

   stream.clear (stream.failbit);
   std::cout << "failbit:"; print_state(stream); std::cout << '\n';

   stream.clear (stream.badbit);
   std::cout << " badbit:"; print_state(stream); std::cout << '\n';

   return 0;
}

Let us compile and run the above program, this will produce the following result −

goodbit: good()=1 eof()=0 fail()=0 bad()=0
 eofbit: good()=0 eof()=1 fail()=0 bad()=0
failbit: good()=0 eof()=0 fail()=1 bad()=0
 badbit: good()=0 eof()=0 fail()=1 bad()=1
ios.htm
Advertisements