
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 |
failbit | Logical error on i/o operation | false |
false |
true |
false |
failbit |
badbit | Read/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; }