Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- using namespace std;
- //Creating a class student
- class student
- {
- private:
- int roll_no;
- char name[100];
- char dept_name[100];
- int marks[5];
- public:
- void get_input();
- void calculate();
- void print_data();
- void filein(int);
- void fileout();
- int countstudent();
- };
- //This function is used to read the student records from file Student.dat
- void student::filein(int pn)
- {
- ifstream infile;
- infile.open("Student.dat",ios::binary);
- infile.seekg(pn*sizeof(student));
- infile.read((char*) this,sizeof(*this));
- }
- //This function is used to write the student records into file Student.dat. If previous data exists new records are appended
- void student::fileout()
- {
- ofstream outfile;
- outfile.open("Student.dat",ios::app|ios::binary);
- get_input();
- outfile.write((char*)this,sizeof(*this));
- }
- //This function is used to count number of students recorded in the file
- int student::countstudent()
- {
- ifstream infile;
- infile.open("Student.dat",ios::binary);
- infile.seekg(0,ios::end);
- int n=infile.tellg()/sizeof(student);
- return n;
- }
- //This function is used to get all the inputs
- void student::get_input()
- {
- cout<<"\nEnter roll-no:";
- cin>>roll_no;
- cout<<"\nEnter name:";
- getchar();
- cin.getline(name,100);
- cout<<"\nEnter department name:";
- cin.getline(dept_name,100);
- }
- //This function is used to print all the information
- void student::print_data()
- {
- cout<<"\n\nRoll:"<<roll_no;
- cout<<"\n\nName:"<<name;
- cout<<"\n\nDepartment:"<<dept_name;
- }
- int main()
- {
- student s;
- int n;
- char ch;
- do
- {
- cout<<"Enter data for student\n-----------------------------------------\n";
- s.fileout();//Read from user and write to file
- cout<<"Enter another(Y/N):";
- cin>>ch;
- }while(ch=='Y'||ch=='y');
- n=s.countstudent();
- for(int i=n-1;i>=0;i--)
- {
- cout<<"\n\nStudent "<<i+1<<"\n----------------";
- s.filein(i);//Read from file
- s.print_data();//Print records in reverse order
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement