saves person object to 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 getData(){  
         cout << "Enter name: "; cin >> name;  
         cout << "Enter age: "; cin >> age;  
         }  
};  
int main(){  
   person pers;   
   pers.getData();
                  
   ofstream outfile("PERSON.DAT", ios::binary);  
   outfile.write(reinterpret_cast<char*>(&pers), sizeof(pers));  
   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