checks for errors opening file : file IO Exception « File Stream « C++ Tutorial






#include <fstream>
#include <iostream>  
using namespace std;  
  
int main(){  
   ifstream file;  
   file.open("a:test.dat");  
  
   if( !file )  
      cout << "\nCan't open GROUP.DAT";  
   else  
      cout << "\nFile opened successfully.";  
   cout << "\nfile = " << file;  
   cout << "\nError state = " << file.rdstate();  
   cout << "\ngood() = " << file.good();  
   cout << "\neof() = " << file.eof();  
   cout << "\nfail() = " << file.fail();  
   cout << "\nbad() = " << file.bad() << endl;  
   file.close();  
   return 0;  
}








12.22.file IO Exception
12.22.1.Read a file in try catch block
12.22.2.Handle basic exception
12.22.3.throw exception in function cascading
12.22.4.checks for errors opening file
12.22.5.handles errors during input and output