Make Your Search Easy ! :) Use me

Friday, February 17, 2017

To modify a record stored in a file :-

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>

class student
   {
  int roll;
  char name[20];
      public:
      void get_roll()
      {
     cout<<"\n enter new roll";
     cin>>roll;
      }
      void get_name()
       {
          cout<<"\n enter new name";
            gets(name);
         }

  void show()
   {
    cout<<"\nRoll :"<<roll<<endl;
    cout<<"\nName :"<<name<<endl;
   }
      int ret_roll()
       {
          return(roll);
         }
 };

void main()
   {
  student s;
  fstream fin("stu.dat",ios::in|ios::out);
  int ch,count=0,pos,r;
  cout<<"\nenter roll to modify : ";
  cin>>r;
  while(fin)
     {
    fin.read((char*)&s,sizeof s);
    if(s.ret_roll()==r)
       {
      cout<<"\ncurrent record is :-\n" ;
      s.show();
      cout<<"\npress 1 to edit name : ";
                  cout<<"\npress 2 to edit roll : ";
      cin>>ch;
      break;
       }
   }
  if(ch==1)
       {
    s.get_name();
   }
  else if(ch==2)
   {
    s.get_roll();
   }
  fin.seekg(-1*sizeof(s),ios::cur);
  fin.write((char*)&s,sizeof s);
  cout<<"\nrecord modified sucessfully\n";
      fin.seekg(-1*sizeof(s),ios::cur);
      fin.read((char*)&s,sizeof s);
      s.show();
  fin.close();
  getch();
 }

No comments:

Post a Comment