C++ program : To read and write two files simultaneously




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

class record
{
   int id;
   char name[50];
   fstream f1,f2;
   public:
   record()
   {

   }
   void get()
   {
      cout<<"Enter id:";
      cin>>id;
      cout<<"Enter Name:";
      cin>>name;
   }
   void read_file()
   {       f1.open("id.txt",ios::in);
      f2.open ("name.txt",ios::in);
      f1>>id;
      f2>>name;
      f1.close();
      f2.close();
   }
   void write_file()
   {
      f1.open("id.txt",ios::out);
      f2.open("name.txt",ios::out);
      f1<<"\n"<<id;
      f2<<"\n"<<name;
      f1.close();
      f2.close();
   }
   void show()
   {
      cout<<"\nId  : "<<id;
      cout<<"\nName: "<<name;
   }

};
int main()
{
   clrscr();
   int x=1;
   record ob;
   do{
      cout<<"SELECT OPTION\n1.Add\n2.Read from file\n3.Show\n4.Write to file\n5.Exit";
      int op;
      cin>>op;
      switch(op)
      {
         case 1: ob.get();break;
         case 2: ob.read_file();break;
         case 3: ob.show();break;
         case 4: ob.write_file();break;
         case 5: x=0;cout<<"press any key to exit";break;
         default:cout<<"Invalid try again";
      }
      getch();
   }while(x);
       return 0;
}

Class diagram 

(click image to view large)

 

0 comments:

Post a Comment