std::basic_ios::operator!(3)
π 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
- 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