std::basic_ios::fail(3)
β¨ 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
eofbitfalse,failbitfalse,badbitfalseβgood()true,fail()false,bad()false,eof()false,operator booltrue,operator!falseeofbitfalse,failbitfalse,badbittrueβgood()false,fail()true,bad()true,eof()false,operator boolfalse,operator!trueeofbitfalse,failbittrue,badbitfalseβgood()false,fail()true,bad()false,eof()false,operator boolfalse,operator!trueeofbitfalse,failbittrue,badbittrueβgood()false,fail()true,bad()true,eof()false,operator boolfalse,operator!trueeofbittrue,failbitfalse,badbitfalseβgood()false,fail()false,bad()false,eof()true,operator booltrue,operator!falseeofbittrue,failbitfalse,badbittrueβgood()false,fail()true,bad()true,eof()true,operator boolfalse,operator!trueeofbittrue,failbittrue,badbitfalseβgood()false,fail()true,bad()false,eof()true,operator boolfalse,operator!trueeofbittrue,failbittrue,badbittrueβgood()false,fail()true,bad()true,eof()true,operator boolfalse,operator!true
ngl the one to memorise: the only row wheregood()istrueis the all-falserow, andbad()mirrorsbadbitwhileeof()mirrorseofbit.fail()istruewheneverfailbitorbadbitis 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
- 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