binary input and output with integers : Binary File « File « C++






binary input and output with integers

  
#include <fstream>            
#include <iostream>  
using namespace std;  
const int MAX = 100;          
int buff[MAX];                
  
int main(){  
   for(int j=0; j<MAX; j++)   
      buff[j] = j;              
                              
   ofstream os("edata.dat", ios::binary);  
                              
   os.write( reinterpret_cast<char*>(buff), MAX*sizeof(int) );  
   os.close();                
  
   for(int j=0; j<MAX; j++)       
      buff[j] = 0;  
                              
   ifstream is("edata.dat", ios::binary);  

   is.read( reinterpret_cast<char*>(buff), MAX*sizeof(int) );  

   for(int j=0; j<MAX; j++){      
      if( buff[j] != j ){ 
         cerr << "Data is incorrect\n"; return 1;
      }
   }     
   cout << "Data is correct\n";  
   return 0;  
}
  
    
  








Related examples in the same category

1.Write numbers to a binary file and read them back
2.reading a complete binary file
3.Binary Files
4.seeks particular person in file