C++ ofstream Write person object to disk

Description

C++ ofstream Write person object to disk

#include <fstream>
#include <iostream>
using namespace std;
class person/*from  w w w. j a  v  a 2 s  .  c  o m*/
{
   protected:
   char name[80];              //person's name
   short age;                  //person's age
   public:
   void getData()              //get person's data
   {
      cout << "Enter name: "; cin >> name;
      cout << "Enter age: "; cin >> age;
   }
};
int main()
{
   person pers;
   pers.getData();                //get data for person
   //create ofstream object
   ofstream outfile("PERSON.DAT", ios::binary);
   outfile.write(reinterpret_cast<char*>(&pers), sizeof(pers));
   return 0;
}



PreviousNext

Related