std::basic_ios::good(3)
π std::basic_ios::good(3) β C++ Standard Library, bestie edition
okay so picture this: you're deep in the C++ Standard Library (std::basic_ios::good, to be exact) and you just wanna know if your stream is doing okay. That's it. That's the function. Let me put you on. π«Ά
β¨ NAME β¨
std::basic_ios::good β which is, and I cannot stress this enough, literally std::basic_ios::good. iconic self-reference, no notes. π
πΈ Synopsis πΈ
bool good() const;
Returns true if the most recent I/O operation on the stream completed successfully. π
Specificalmente, it returns the result of rdstate() == 0. That's the whole vibe check: zero flags set = good. β¨
If you wanna see the full roster of conditions that set the stream status bits, go peek at ios_base::iostate β it's giving the receipts. π
π¦ Parameters π¦
(none) β nothing to pass in, she takes no arguments, she's low maintenance, main character energy fr. π
β Return value β
true if the stream error flags are all false, false otherwise. Simple as that, no cap. π
π₯ Example π₯
Run this code, bestie, it's a whole journey:
#include <cstdlib>
#include <fstream>
#include <iostream>
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
π See also π
okay this next bit is the part everyone screenshots and puts in the group chat, because it's the cheat sheet. The following table shows the value of basic_ios accessors (good(), fail(), etc.) for all possible combinations of ios_base::iostate flags. Every. Single. One. Periota. π
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
π tiny icon behaviour: notice howgood()is onlytruein that very first row where literally every flag is false, and notice howeofbitall by itself makesgood()gofalsewhilefail()staysfalseβ the table is right there, bestie, read it, live it, love it. π
alright that's the page!! sourced straight from http://cppreference.com, dated 2024.06.10, for std::basic_ios::good(3) β mwah, go check your error flags, ily, bye π§ββοΈπβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms