C++ IOS Library clear



Description

It is used to set 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).

Declaration

Following is the declaration for ios::clear function.

void clear (iostate state = goodbit);

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 constant)

indicates functions to check state flags
good() eof() fail() bad() rdstate()
goodbit No errors (zero value iostate) true false false false goodbit
eofbit End-of-File reached on input operation false true false false eofbit
failbitLogical error on i/o operation false false true false failbit
badbitRead/writing error on i/o operation false false true true badbit

Return Value

none

Exceptions

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.

Data Races

Modifies the stream object.

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

Example

In below example explains about ios::clear function.

#include <iostream>
#include <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;
}
ios.htm
Advertisements