#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
Class Student { char
name[40];
char grade;
float marks;
public:
void getdata(void);
void display(void); };
void Student ::
getdata(void)
{ char ch;
cin.get(ch);
cout<<”Enter name: “; cin.getline(name,40);
cout<<”Enter Grade: ”; cin>>grade;
cout<<”Enter Marks: “; cin>>marks;
cout<<”\n”;
}
Void
Student::display(void)
{ cout<<”Name:”<<name<<”\t”
cout<<”Grade:”<<grade<<”\t”
cout<<”Marks:”<<marks<<”\t” <<”\n”;
}
Int main()
{ system(“cls”);
Student arts[3]; //declare
array of 3 objects
fstream filin; //input
and output file
filin.open(“Stu.dat”,ios::in|ios::out);
if(!filin)
{
cout << “Can not open file!!\n”;
Return 1;
}
Cout <<”Enter details for 3 students \n”;
for(int i=0;I<3;i++)
{ arts[i].getdata();
filin.write((char *) & arts[i],
sizeof (arts[i]));
}
Filin.seekg(0); //seek
0 resets the file to start,so that the file can
//be
accessed from the beginning.
cout<<”The contents of stu.dat are shown
below.\n”;
for(i=0;i<3;i++)
{ filin.read((char
*)&arts[i],sizeof(arts[i]));
Arts[i].display();
}
Filin.close();
Return 0;
}
No comments:
Post a Comment