#include < fstream.h > #include < conio.h > #include < io.h > class student{ private: int no; char name[25]; float fee; public: void getdata() { cout << "\n\n Roll Number : " ; cin >> no; cout << "\n\n Name : "; cin >> name; cout << "\n\n Fees : "; cin >> fee; } void putdata() { cout << "\n\n Roll Number : " << no; cout << "\n\n Name : " << name; cout << "\n\n Fees : " << fee; } }; void main() { student s1; fstream stdfile; clrscr(); stdfile.open("student.dat",ios::out|ios::in|ios::binary); char ch; do { s1.getdata(); stdfile.write((char *)&s1,sizeof(s1)); cout<<"\n Continue? y/n : "; cin>>ch; } while (ch=='Y'||ch=='y'); clrscr(); stdfile.seekg(0,ios::end); //moves the get pointer to the end of file int endposition=stdfile.tellg(); //returns current position of get or put pointer in bytes int n=endposition/sizeof(s1); //So you will get the total number of records cout << "\n\n There are " << n <<" records in the file \n"; int num; cout << "\n\n Enter the number of the record which want to read : "; cin >> num; int position=(num-1)*sizeof(s1); stdfile.seekg(position); stdfile.read((char *)&s1,sizeof(s1)); s1.putdata(); getch(); }Click here to go back.
Our aim is to provide information to the knowledge seekers.