std::basic_ios::good(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 how good() is only true in that very first row where literally every flag is false, and notice how eofbit all by itself makes good() go false while fail() stays false β€” 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