/*PROGRAM : Reading and Writing objects*/
//File handling
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<iostream.h>
class person
{
protected:
char name[80];
int age;
public:
void get_data() //get person's data from user
{
cout<<"\nEnter name : ";
cin>>name;
cout<<"\nEnter age : ";
cin>>age;
}
void show_data() //display person's data
{
cout<<"\nNAME : "<<name;
cout<<"\nAGE : "<<age;
}
};
void main()
{
char ch;
clrscr();
person p1;
fstream file; //create input/output file
file.open("sample.dat",ios::app | ios::out | ios::in | ios::binary);
//data from user to file
do
{
cout<<"\nEnter person's data :";
p1.get_data();
file.write((char *)&p1,sizeof(p1)); //write to file
cout<<"\nWant to enter more details (y/n) ?";
cin>>ch;
}while(ch=='y');
file.seekg(0); //reset to start of file
file.read((char *)&p1,sizeof(p1)); //read first person data
while(!file.eof())
{
cout<<"\n";
p1.show_data();
file.read((char *)&p1,sizeof(p1));
}
cout<<endl;
getch();
}