Pages

Sunday, November 11, 2012

c++ program for reading and writing of class objects


Here is an example for reading and writting of class object in c++ : 
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class student
{
int roll_no;
char name[20];
public:
void read()
{
cin>>roll_no>>name;
}
void show()
{
cout<<roll_no<<name;
}
};
void main()
{
clrscr();
student s1;
s1.read();
ofstream out("stu.txt",ios::binary);
out.write((char*)&s1,sizeof(s1));
ifstream in("stu.txt",ios::binary);
in.read((char*)&s1,sizeof(s1));
s1.show();
getch();
}

Output of program is:


1 comment: