cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : ios : clear
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
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 falgs 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

// 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)
ios::rdstate Get error state flags (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us