Advertisement
Guest User

q18

a guest
Apr 26th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. /* Question Number 18: File I/O */
  2. #include <fstream>
  3. #include <iostream>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9.    int ch,flag=0,flag2;
  10.    char data[50],src[50],phn[50];
  11.    while (1)
  12.    {
  13.     cout<<"\nMenu\n1.Add\n2.Delete\n3.Search\n4.Save & Display\n5.Exit\nEnter your choice: ";
  14.     cin>>ch;
  15.     switch(ch)
  16.     {
  17.     case 1:
  18.     {
  19.         ofstream outfile;
  20.             outfile.open("afile.txt",ios::app);
  21.             cout << "\nEnter your name: ";
  22.         cin.ignore();
  23.             cin.getline(data,20);
  24.             outfile << data << ' ';
  25.             cout << "Enter your Phone: ";
  26.         cin.ignore();
  27.         cin.getline(phn,10);
  28.                 outfile<< phn << endl;
  29.                 outfile.close();
  30.         break;
  31.     }
  32.     case 2:
  33.     {
  34.         ifstream infile("afile.txt");
  35.             ofstream outfile("temp.txt");
  36.             cout << "\nEnter name you want to erase from database: ";
  37.         cin.ignore();
  38.         cin.getline(src,20);
  39.         while(infile>>data>>phn)
  40.             {
  41.                 if(strcmp(data,src))
  42.             {
  43.                 flag2=0;
  44.                         outfile << data <<' '<< phn <<endl;
  45.             }
  46.                 else
  47.                     flag2=1;
  48.             }
  49.             infile.close();
  50.             outfile.close();
  51.             remove("afile.txt");
  52.             rename("temp.txt","afile.txt");
  53.             if(flag2==0)
  54.                 cout<<"There is no record with the name you entered"<<endl;
  55.             else
  56.                 cout<<"Record has been deleted." << endl;
  57.             break;
  58.     }
  59.     case 3:
  60.     {
  61.         ifstream infile;
  62.         infile.open("afile.txt");
  63.         cout<<"\nEnter name of person to be searched: ";
  64.         cin.ignore();
  65.         cin.getline(src,20);
  66.         while(infile>>data>>phn)
  67.         {
  68.             if(strstr(data,src))
  69.             {
  70.                 flag=1;
  71.                 break;
  72.             }
  73.             else
  74.                 flag=0;
  75.         }
  76.         if (flag==1)
  77.         {
  78.             cout<<"\nRecord found!!!";
  79.             cout<<"Name: "<<data<<"\nPhone: "<<phn<<endl;
  80.         }
  81.         else
  82.             cout<<"\nRecord not found";
  83.         infile.close();
  84.         break;
  85.     }
  86.     case 4:
  87.     {
  88.         ifstream infile;
  89.         infile.open("afile.txt");
  90.         while(infile>>data>>phn)
  91.             cout<<"Name: "<<data<<"\nPhone: "<<phn<<endl;
  92.         break;
  93.     }
  94.     //break;
  95.     case 5: return 0;
  96.     }
  97.   }
  98.   return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement