Read person object from disk : class save load « File Stream « C++ Tutorial






#include <fstream>                
#include <iostream>  
using namespace std;  

class person{  
   protected:  
      char name[80];              
      short age;                  
   public:  
      void showData(){  
         cout << "Name: " << name << endl;  
         cout << "Age: " << age << endl;  
      }  
};  

int main(){  
   person pers;                   
   ifstream infile("PERSON.DAT", ios::binary); 
                                  
   infile.read( reinterpret_cast<char*>(&pers), sizeof(pers) );  
   pers.showData();               
   return 0;  
}








12.4.class save load
12.4.1.Save class to a file
12.4.2.Writing a class to a file.
12.4.3.Read class data from file
12.4.4.Writing a Class to a File
12.4.5.Read person object from disk
12.4.6.saves person object to disk
12.4.7.reads and writes several objects to disk