ios fail() function in C++ with Examples Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report The fail() method of ios class in C++ is used to check if the stream is has raised any fail error. It means that this function will check if this stream has its failbit set. Syntax: bool fail() const; Parameters: This method does not accept any parameter. Return Value: This method returns true if the stream has failbit set, else false. Example 1: CPP // C++ code to demonstrate // the working of fail() function #include <bits/stdc++.h> using namespace std; int main() { // Stream stringstream ss; // Using fail() function bool isFail = ss.fail(); // print result cout << "is stream fail: " << isFail << endl; return 0; } Output: is stream fail: 0 Example 2: CPP // C++ code to demonstrate // the working of fail() function #include <bits/stdc++.h> using namespace std; int main() { // Stream stringstream ss; ss.clear(ss.failbit); // Using fail() function bool isFail = ss.fail(); // print result cout << "is stream fail: " << isFail << endl; return 0; } Output: is stream fail: 1 Reference: hhttps://cplusplus.com/reference/ios/ios/fail/ Create Quiz Comment G guptayashgupta53 Follow 1 Improve G guptayashgupta53 Follow 1 Improve Article Tags : Misc C++ CPP-Functions cpp-ios Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like