bool good() const; |
Returns true
if the most recent I/O operation on the stream completed successfully. Specifically, returns result of rdstate() == 0
.
See ios_base::iostate
for the list of conditions that set the stream status bits.
(none).
true
if the stream error flags are all false, false
otherwise.
#include <iostream> #include <fstream> #include <cstdlib> int main() { const char* fname = "/tmp/test.txt"; std::ofstream ofile{fname}; ofile << "10 " << "11 " << "12 " << "non-int"; ofile.close(); std::ifstream file{fname}; if (!file.good()) { std::cout << "#1. Opening file test.txt failed - " "one of the error flags is true\n"; return EXIT_FAILURE; } // typical C++ I/O loop uses the return value of the I/O function // as the loop controlling condition, operator bool() is used here for (int n; file >> n; ) { std::cout << n << ' '; } std::cout << '\n'; if (file.bad()) { std::cout << "#2. I/O error while reading - badbit is true\n"; return EXIT_FAILURE; } else if (file.eof()) { std::cout << "#3. End of file reached successfully - eofbit is true\n" "This is fine even though file.good() is false\n"; } else if (file.fail()) { std::cout << "#4. Non-integer data encountered - failbit is true\n"; } }
Possible output:
10 11 12 #4. Non-integer data encountered - failbit is true
The following table shows the value of basic_ios
accessors (good()
, fail()
, etc.) for all possible combinations of ios_base::iostate
flags:
ios_base::iostate flags | basic_ios accessors |
|||||||
eofbit | failbit | badbit | good() | fail() | bad() | eof() | operator bool | operator! |
false | false | false | true | false | false | false | true | false |
false | false | true | false | true | true | false | false | true |
false | true | false | false | true | false | false | false | true |
false | true | true | false | true | true | false | false | true |
true | false | false | false | false | false | true | true | false |
true | false | true | false | true | true | true | false | true |
true | true | false | false | true | false | true | false | true |
true | true | true | false | true | true | true | false | true |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/io/basic_ios/good