| get (1) | iostate exceptions() const; |
|---|---|
| set (2) | void exceptions (iostate except); |
clear() was called).| iostate value (member constants) | 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 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// basic_ios::exceptions
#include <iostream> // std::cerr
#include <fstream> // std::ifstream
int main () {
std::ifstream file;
file.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
try {
file.open ("test.txt");
while (!file.eof()) file.get();
}
catch (std::ifstream::failure e) {
std::cerr << "Exception opening/reading file";
}
file.close();
return 0;
}