|
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 value | indicates |
eofbit | End-Of-File reached while performing an extracting operation on an input stream. |
failbit | The last input operation failed because of an error related to the internal logic of the operation itself. |
badbit | Error due to the failure of an input/output operation on the stream buffer. |
goodbit | No 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
ios::fail | Check if either failbit or badbit is set (public member function) |
ios::good | Check if the state of the stream is good for i/o operations. (public member function) |
ios::bad | Check if badbit is set (public member function) |
ios::eof | Check if eofbit is set (public member function) |
|