handles errors during input and output : file IO Exception « File Stream « C++ Tutorial






#include <fstream>
#include <iostream>  
#include <process.h>
using namespace std;  
  
const int MAX = 1000;  
int buff[MAX];  
  
int main(){  
   
   for(int j=0; j<MAX; j++)
      buff[j] = j;  
   ofstream os;            
                           
   os.open("a:edata.dat", ios::trunc | ios::binary);  
   if(!os){ 
      cerr << "Could not open output file\n"; exit(1); 
   }  
  
   os.write( reinterpret_cast<char*>(buff), MAX*sizeof(int) );  
   if(!os){ 
      cerr << "Could not write to file\n"; exit(1); 
   }  
   os.close();             
  
   for(int j=0; j<MAX; j++)    
      buff[j] = 0;  
  
   ifstream is;            
   is.open("a:edata.dat", ios::binary);  
   if(!is){ 
      cerr << "Could not open input file\n"; exit(1); 
   }  
  
   is.read( reinterpret_cast<char*>(buff), MAX*sizeof(int) );  
   if(!is){ 
      cerr << "Could not read from file\n"; exit(1); 
   }  
  
   for(int j=0; j<MAX; j++){
      if( buff[j] != j ){ 
         cerr << "\nData is incorrect\n"; 
         exit(1); 
      }  
   }
   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