u

Saturday 2 June 2012

C++ Program implementing File Operations

Program implementing File Operations
 Assuming the class student, write functions to implement the following :
(i). Store the objects of student in a binary file.
(ii). Read the object of student from binary file and print the same on the screen.

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class student
{
int rollno;
char name[20];
public:
void getval()
{
cin>>rollno;
gets(name);
}
void putval()
{
cout<<rollno;
cout<<endl<<name;
}
};
main()
{
student ward;
clrscr();
cout<<"Enter roll number and name of the record"<<endl;
ward.getval();
ofstream fout;
fout.open("a.dat",ios::binary);
fout.write((char*)&ward,sizeof(ward));
fout.close();
ifstream fin;
fin.open("a.dat",ios::in|ios::binary);
fin.read((char*)&ward,sizeof(ward));
cout<<endl;
cout<<"The record read is "<<endl;
ward.putval();
cout<<"Press any key to continue";
getch();
return 0;
}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More