1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
class student
{
int rollno,marks;
char name[20];
public:
void show();
void get();
int write(const student &,fstream &,int);
int read(const student &,fstream &,int);
};
void student::get()
{
cout<<"Enter the name of student:";
cin>>name;
cout<<"Enter the roll no. of student:";
cin>>rollno;
cout<<"Enter the marks of student:";
cin>>marks;
}
int student::write(const student &s,fstream &fp,int RecNum)
{
if( fp.seekp(RecNum*sizeof(s), ios::beg) == 0 )
if(fp.write((char*)&s,sizeof s))
return 1;
return 0;
}
int student::read(const student &s,fstream &fp,int RecNum)
{
if( fp.seekg(RecNum*sizeof(s), ios::beg) == 0 )
if(fp.read((char*)&s,sizeof s))
return 1;
return 0;
}
int main()
{
student record;
fstream file;
file.open("student.txt",ios::in|ios::out|ios::binary|ios::app);
char flag='y';
int i=0;
record.get();
record.write(record,file,2);
cout<<"Roll No.\tName\tMarks"<<endl;
record.read(record,file,2);
file.close();
return 0;
}
void student::show()
{
cout<<rollno<<"\t\t"<<name<<"\t"<<marks<<endl;
}
|