File program

Program to input student details and store it in a file..
The program performs following operations.
1 : Print the total no of student details entered into the file
2 : Print the student details entered.
3 : Update a particular student detail.


#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class student
{
char name[10];
int usn,sem,total;
public:void read();
void disp();
};
void student::read()
{
cout<<"Enter name,usn,sem,total : ";
cin>>name>>usn>>sem>>total;
}
void student::disp()
{
cout<<"Name \t"<<name<<"\n"<<"Usn\t"<<usn<<"\n"<<"Sem\t "<<sem<<"\n"<<"Total\t"<<total<<"\n";
}
main()
{
int i;
student s1,s[10];
fstream fp;
clrscr();
fp.open("bio.text",ios::ate|ios::in|ios::out|ios::binary);
cout<<"Enter student details \n";
for(i=1;i<=3;i++) /*Input of dtails into the file*/
{
s[i].read();
fp.write((char*)&s[i],sizeof(s[i]));
}
fp.seekg(0);
cout<<"Details\n\n"; /*Printing the details*/
for(i=1;i<=3;i++)
{
fp.read((char*)&s[i],sizeof(s[i]));
s[i].disp();
cout<<"\n";
}
int pt=fp.tellg();
int n=pt/sizeof(*s);
cout<<"No of student details entered : "<<n<<"\n";
cout<<"Enter object no to be updated : "; /* To update the current existing details of objects*/
int obj;
cin>>obj;
int loc=(obj-1)*sizeof(s1);
fp.seekp(loc);
cout<<"Enter new values \n";
s1.read();
fp.write((char*)&s1,sizeof(s1));
fp.seekg(0);
cout<<"Details\n\n"; /*Final print after the update*/
for(i=1;i<=3;i++)
{
fp.read((char*)&s[i],sizeof(s[i]));
s[i].disp();
cout<<"\n";
}
fp.close();
getch();
return 0;
}
Category:

1 comments:

Anonymous said...

thanks boss