std::basic_ios::bad(3)

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

πŸ’… std::basic_ios::bad πŸ’…

okay so welcome to the C++ Standard Libary corner of the group chat, bestie β€” this is std::basic_ios::bad(3), and we’re gonna get you fluent in it, no cap. πŸ’–βœ¨

🌸 NAME 🌸

std::basic_ios::bad β€” std::basic_ios::bad


✨ Synopsis ✨

bool bad() const;

This absolute icon returns true if a non-recoverable error has occurred on the associated stream. Specifically, it returns true if badbit is set in rdstate(). πŸ‘‘

See ios_base::iostate for the list of conditions that set badbit. iykyk πŸ’…


πŸŽ€ Parameters πŸŽ€

Parameters: (none) β€” literally zero arguments, she takes none, periodt. 🫢


πŸ’– Return value πŸ’–

true if a non-recoverable error has occurred, false otherwise. That’s the whole vibe check, bestie. 🌈


πŸ§ͺ Example πŸ§ͺ

okay so run this code, fr fr:

// 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";
}

πŸ‘€ See also πŸ‘€

The following table shows the value of basic_ios accessors (good(), fail(), etc.) for all possible combinations of ios_base::iostate flags:

  • 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

shoutout to http://cppreference.com for the source/version, dated 2024.06.10, and this was std::basic_ios::bad(3) β€” mwah, bye bestie! πŸ’‹βœ¨πŸ«Ά

πŸ“¦ where this page comes from