#include<iostream.h>
#include<process.h>
#include<fstream.h>
#include<conio.h>
fstream file;

class student
 {
  int rollno;
  char name[20];
  int marks;

  public:
  void getdata()
   {
cout<<"\n Enter the roll: ";
cin>>rollno;
cout<<"\nEnter the name: ";
cin>>name;
cout<<"\nEnter marks(out of 100): ";
cin>>marks;
   }

  void display(int n)
   {
for(int i=0;i<n;i++)
{
file.read((char*)&s[i],sizeof(s[i]));
cout<<"\n Rollno:"<<s[i].rollno;
cout<<"\n Name:"<<s[i].name;
cout<<"\n Marks:"<<s[i].marks;
   }
  }
}s[10];

int main()
 {
  int n,choice;
  clrscr();
  do
  {
  cout<<endl<<"1.Accept details of n students";
  cout<<endl<<"2.Read and Display details from file";
  cout<<endl<<"3.Update given record in file.";
  cout<<endl<<"4.Exit";
  cout<<endl<<"Enter choice: ";
  cin>>choice;
  switch(choice)
  {
  case 1:
  cout<<endl<<"Enter number of students: ";
  cin>>n;
  file.open("D:/BCA/school.txt",ios::in|ios::out);
  for(int i=0;i<n;i++)
  {
   s[i].getdata();
   file.write((char*)&s[i],sizeof(s[i]));
  }
  break;

  case 2:
  file.seekg(0);
  cout<<"\n Contents of file are:";
s->display(n);
  break;

  case 3:
  int up;
  cout<<endl<<"Enter the record number to be updated: ";
  cin>>up;
  for(i=0;i<up-1;i++)
  {
  file.read((char*)&s[i],sizeof(s[i]));
  }
  file.read((char*)&s[i],sizeof(s[i]));
  file.seekp((up-1)*sizeof(s[i]));
  s[up-1].getdata();
  file.write((char*)&s[up-1],sizeof(s[up-1]));
  file.seekg(0);
  s->display(n);
  break;

  case 4:
  exit(0);
  break;

  default:
  cout<<endl<<"Wrong choice!";
  }
 }while(choice!=4);
 getch();
 return 0;
 }

1 Comments

Post a Comment

Previous Post Next Post