std::basic_ios::fail(3)

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

✨ std::basic_ios::fail πŸ’…

okay bestie so we're in the C++ Standard Library manual rn and this little icon is std::basic_ios::fail β€” lowercase, no drama, just a bool-returning vibe check for your streams 🫢


🌸 Synopsis 🌸

bool fail() const;

Returns true if an error has occurred on the associated stream. Specifically, returns true if badbit or failbit is set in rdstate(). 😭

And if you wanna know exactly which conditions set failbit or badbit, go peek at ios_base::iostate β€” it's got the full list, iykyk ✨


πŸŽ€ Parameters πŸŽ€

(none) β€” literally nothing, she takes no arguments, she's low maintenance πŸ’…


πŸ¦‹ Return value πŸ¦‹

true if an error has occurred, false otherwise. That's the whole personality. Periodt. πŸ‘‘


πŸ”₯ Example πŸ”₯

// Run this code

#include <cstdlib>
#include <fstream>
#include <iostream>

int main()
{
    std::ifstream file("test.txt");
    if (!file) // operator! is used here
    {
        std::cout << "File opening failed\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 << "I/O error while reading\n";
    else if (file.eof())
        std::cout << "End of file reached successfully\n";
    else if (file.fail())
        std::cout << "Non-integer data encountered\n";
}

like, the way this example eats?? operator! for the file-open check, operator bool() for the loop condition, then bad() / eof() / fail() in a little cascade at the end β€” main character energy fr fr πŸŒˆπŸ‘‘


⭐ See also ⭐

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 false, failbit false, badbit false β†’ good() true, fail() false, bad() false, eof() false, operator bool true, operator! false
  • eofbit false, failbit false, badbit true β†’ good() false, fail() true, bad() true, eof() false, operator bool false, operator! true
  • eofbit false, failbit true, badbit false β†’ good() false, fail() true, bad() false, eof() false, operator bool false, operator! true
  • eofbit false, failbit true, badbit true β†’ good() false, fail() true, bad() true, eof() false, operator bool false, operator! true
  • eofbit true, failbit false, badbit false β†’ good() false, fail() false, bad() false, eof() true, operator bool true, operator! false
  • eofbit true, failbit false, badbit true β†’ good() false, fail() true, bad() true, eof() true, operator bool false, operator! true
  • eofbit true, failbit true, badbit false β†’ good() false, fail() true, bad() false, eof() true, operator bool false, operator! true
  • eofbit true, failbit true, badbit true β†’ good() false, fail() true, bad() true, eof() true, operator bool false, operator! true
ngl the one to memorise: the only row where good() is true is the all-false row, and bad() mirrors badbit while eof() mirrors eofbit. fail() is true whenever failbit or badbit is chillin' in there. slay πŸ’–
  • ferror β€” checks for a file error (function) 🌸

okay that's the whole page bestie, no cap, every single bit of it πŸ’œ This cutie's from cppreference.com, dated 2024.06.10 β€” catch you on the next manpage, mwah πŸŽ€πŸ’…βœ¨

πŸ“¦ where this page comes from