Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
IOstream Library
manipulators
classes:
filebuf
fstream
ifstream
ios
iostream
ios_base
istream
istringstream
ofstream
ostream
ostringstream
streambuf
stringbuf
stringstream
objects:
cerr
cin
clog
cout
types:
fpos
streamoff
streampos
streamsize
ios
ios::ios
ios::~ios
member functions:
ios::bad
ios::clear
ios::copyfmt
ios::eof
ios::exceptions
ios::fail
ios::fill
ios::good
ios::imbue
ios::init
ios::narrow
ios::operator!
ios::operator void*
ios::rdbuf
ios::rdstate
ios::setstate
ios::tie
ios::widen


ios::clear

public member function
void clear ( iostate state = goodbit );

Set error state flags

Sets a new value for the error control state.

All the bits in the control state are replaced by the new ones; The value existing before the call has no effect.

If the function is called with goodbit as argument (which is the default value) all error flags are cleared.

The current state can be obtained with member function rdstate.

Parameters

state
An object of type ios_base::iostate that can take as value any combination of the following state flag member constants:


flag valueindicates
eofbitEnd-Of-File reached while performing an extracting operation on an input stream.
failbitThe last input operation failed because of an error related to the internal logic of the operation itself.
badbitError due to the failure of an input/output operation on the stream buffer.
goodbitNo error. Represents the absence of all the above (the value zero).

These values are declared as static member constants in the parent class ios_base.

More than one state flag can be combined using the bitwise | (or) operator.

If this parameter is not specified, goodbit is assumed, so that any existing error state value is cleared.

Return Value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// clearing errors
#include <iostream>
#include <fstream>
using namespace std;
int main () {
  char buffer [80];
  fstream myfile;
  myfile.open ("test.txt",fstream::in);
  myfile << "test";
  if (myfile.fail())
  {
    cout << "Error writing to test.txt\n";
    myfile.clear();
  }
  myfile.getline (buffer,80);
  cout << buffer << " successfully read from file.\n";
  return 0;
}


In this example myfile is open for input operations, but we perform an output operation on it, so failbit is set. The example calls then clear in order to remove the flag and allow further operations like getline to be successfully performed on myfile.

Basic template member declaration

( basic_ios<charT,traits> )
 
void clear ( iostate state = goodbit );


See also