Saves person object to disk - C++ File Stream

C++ examples for File Stream:stream

Description

Saves person object to disk

Demo Code

#include <fstream>
#include <iostream>
using namespace std;
class person//from   w  w w  .ja v  a  2  s.  co  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;
}

Result


Related Tutorials