std::basic_ios::operator!(3)

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

πŸ’– NAME πŸ’–

std::basic_ios::operator! β€” the NAME line is literally std::basic_ios::operator! - std::basic_ios::operator!, which is giving β€œI contain multitudes” energy. This is the std::basic_ios::operator!(3) page from the C++ Standard Libary man pages. It’s the little negation operator that tells you if your stream is in an error state. πŸ’…

✨ Synopsis ✨

bool operator!() const;

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

🌸 Parameters 🌸

(none) β€” literally none. Zero. The parameter list is empty, bestie. πŸŽ€

πŸŽ€ Return value πŸŽ€

true if an error has occurred, false otherwise. That’s the whole return-value story. ✨

πŸ¦‹ Example πŸ¦‹

Here’s the example, verbatim, because the code is the main character:

// 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 original says: β€œThe following table shows the value of basic_ios accessors (good(), fail(), etc.) for all possible combinations of ios_base::iostate flags:” Here it is, same values, just in list form so we don’t break the terminal:

  • 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.

That’s the whole page, bestie. Source: http://cppreference.com β€’ version/date: 2024.06.10 β€’ page: std::basic_ios::operator!(3). Slay. πŸ’–

πŸ“¦ where this page comes from