public member function
<ios> <iostream>

std::basic_ios::clear

void clear (iostate state = goodbit);
Set error state flags
Sets a new value for the stream's internal error state flags.

The current value of the flags is overwritten: All bits are replaced by those in state; If state is goodbit (which is zero) all error flags are cleared.

In the case that no stream buffer is associated with the stream when this function is called, the badbit flag is automatically set (no matter the value for that bit passed in argument state).

Note that changing the state may throw an exception, depending on the latest settings passed to member exceptions.

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:

iostate value
(member constants)
indicatesfunctions to check state flags
good()eof()fail()bad()rdstate()
goodbitNo errors (zero value iostate)truefalsefalsefalsegoodbit
eofbitEnd-of-File reached on input operationfalsetruefalsefalseeofbit
failbitLogical error on i/o operationfalsefalsetruefalsefailbit
badbitRead/writing error on i/o operationfalsefalsetruetruebadbit
eofbit, failbit and badbit are member constants with implementation-defined values that can be combined (as if with the bitwise OR operator).
goodbit is zero, indicating that none of the other bits is set.

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
// clearing errors
#include <iostream>     // std::cout
#include <fstream>      // std::fstream

int main () {
  char buffer [80];
  std::fstream myfile;

  myfile.open ("test.txt",std::fstream::in);

  myfile << "test";
  if (myfile.fail())
  {
    std::cout << "Error writing to test.txt\n";
    myfile.clear();
  }

  myfile.getline (buffer,80);
  std::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 attempted on myfile.

Data races

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

Exception safety

Basic guarantee: if an exception is thrown, the stream is in a valid state.
It throws an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set throw for that state.

See also