Advertisement
sherry_ahmos

Untitled

Apr 20th, 2022
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4. using namespace std;
  5. class car
  6. {
  7. public:
  8.     int id,model;
  9.     char name[10],ownerofcar[10];
  10. };
  11. void read(){
  12.     car obj2;
  13.                 ifstream infile;
  14.                 infile.open("car.txt",ios::in);
  15.                 if(infile.is_open())
  16.                 {
  17.                     int id=0;
  18.                     while(! infile.eof())
  19.                     {
  20.                         infile.read((char*)&obj2,sizeof(obj2));
  21.                         if(obj2.id != id)
  22.                         {
  23.                             cout<< obj2.id << "\t" << obj2.model << "\t" << obj2.name << "\t" <<obj2.ownerofcar <<endl;
  24.                             id = obj2.id;
  25.                         }
  26.  
  27.                     }
  28.                 }
  29.                 else cout<< "Can't open the file";
  30.                 infile.close();
  31. }
  32. int main()
  33. {
  34.     char t;
  35.     cout<<"**Welcome**\n";
  36.     do{
  37.         cout<<"Enter the number of opertion you want\n";
  38.         cout<<"1-Write on the file\n";
  39.         cout<<"2-Read from the file\n";
  40.         cout<<"3-search on the file\n";
  41.         cout<<"4-Copy the file\n";
  42.         cout<<"5-length of the file\n";
  43.         cout<<"6-update on the file\n";
  44.         cout<<"7-Delete from the file\n";
  45.         int s;
  46.         cin>>s;
  47.         switch(s){
  48.             case 1:
  49.                 {
  50.                 car obj1;
  51.                 char c;
  52.                 ofstream outfile(("car.txt"));
  53.                 do
  54.                 {
  55.                     cout<< "Enter Car ID: ";
  56.                     cin>>obj1.id;
  57.                     cout<< "Enter Car Model: ";
  58.                     cin>>obj1.model;
  59.                     cout<< "Enter  Car Name: ";
  60.                     cin>>obj1.name;
  61.                     cout<< "Enter Owner Of The Car: ";
  62.                     cin>>obj1.ownerofcar;
  63.                     outfile.write((char*)&obj1,sizeof(obj1));
  64.                     cout<< "Enter another record? (y/n) ";
  65.                     cin>>c;
  66.                 }
  67.                 while(c=='y');
  68.                 outfile.close();
  69.                 cout<<"Do you want to do anothe opertion (y\\n): \n";
  70.                 cin>>t;
  71.                 break;
  72.             }
  73.            
  74.             case 2:
  75.                 {
  76.                 read();
  77.                 cout<<"Do you want to do anothe opertion (y\\n): \n";
  78.                 cin>>t;
  79.                 break;
  80.             }
  81.             case 3:
  82.                 {
  83.                     car obj;
  84.                     bool found=false;
  85.                     char str[10];
  86.                     cout<< "Enter owner name to search for: ";
  87.                     cin>>str;
  88.                     ifstream infile("car.txt");
  89.                     while(!infile.eof())
  90.                     {
  91.                         infile.read((char*)&obj,sizeof(obj));
  92.                         if(strcmp(str,obj.ownerofcar)==0)
  93.                         {
  94.                             found = true;
  95.                             cout<< "ID"  << "\t"<< "Model" << "\t"<< "Name" << "\t"<< "Owner Of Car\n";
  96.                             cout << "----------------------------------" << endl;
  97.                             cout << obj.id << "\t" << obj.model << "\t"<< obj.name << "\t"<<obj.ownerofcar <<endl;
  98.                             break;
  99.                         }
  100.                     }
  101.                     if(!found)
  102.                             cout << "No items matched your search \n";
  103.  
  104.                     infile.close();
  105.                     cout<<"Do you want to do anothe opertion (y\\n): \n";
  106.                     cin>>t;
  107.                     break;
  108.                 }
  109.                 case 4:
  110.                     {
  111.                         char ch;
  112.                         ifstream infile("Car.txt");
  113.                         ofstream outfile("copied file.txt");
  114.                         while(infile.get(ch))
  115.                         {
  116.                         outfile.put(ch);
  117.                         }
  118.                         infile.close();
  119.                         cout<<"Do you want to do anothe opertion (y\\n): \n";
  120.                         cin>>t;
  121.                         break;
  122.                     }
  123.                 case 5:
  124.                     {
  125.                         int ch1;
  126.                         ifstream infile("Car.txt");
  127.                         infile.seekg(0,ios::end);
  128.                         cout<<" length of the file:"<<infile.tellg()<<endl;
  129.                         infile.close();
  130.                         cout<<"Do you want to do anothe opertion (y\\n): \n";
  131.                         cin>>t;
  132.                         break;
  133.                     }
  134.                     case 6:
  135.                 {
  136.                         car obj;
  137.                         bool found=false;
  138.                         char str[10];
  139.                         cout<< "Enter owner name to search for: ";
  140.                         cin>>str;
  141.                         fstream infile("car.txt",ios::in|ios::out);
  142.                         infile.read((char*)&obj,sizeof(obj));
  143.                         while(!infile.eof())
  144.                         {
  145.                         if(strcmp(str,obj.ownerofcar)==0)
  146.                         {
  147.                             cout<<"Enter the new model for "<<str<<" ";
  148.                             cin>>obj.model;
  149.                             int curpos=infile.tellg();
  150.                             int namesize=sizeof(str);
  151.                             infile.seekp(curpos-namesize,ios::beg);
  152.                             infile.write((char*)&obj,sizeof(obj));
  153.                             found = true;
  154.                             //infile.seekg(curpos-namesize,ios::beg);
  155.                             //infile.read((char*)&obj,sizeof(obj));
  156.                             cout<< "ID"  << "\t"<< "Model" << "\t"<< "Name" << "\t"<< "Owner Of Car\n";
  157.                             cout << "----------------------------------" << endl;
  158.                             cout << obj.id << "\t" << obj.model << "\t"<< obj.name << "\t"<<obj.ownerofcar <<endl;
  159.                         break;
  160.                         }
  161.                         //infile.read((char*)&obj,sizeof(obj));
  162.                         }
  163.                     if(!found)
  164.                         cout << "No items matched your search \n";
  165.  
  166.                     infile.close();
  167.                     cout<<"Do you want to do anothe opertion (y\\n): \n";
  168.                     cin>>t;
  169.                         break;
  170.                     }
  171.             case 7:
  172.                 {
  173.                     car obj;
  174.                     bool found=false;
  175.                     char str[10];
  176.                     cout<< "Enter owner name to delete for: ";
  177.                     cin>>str;
  178.                     ifstream infile("car.txt",ios::in);
  179.                     ofstream outfile("temp.txt",ios::out);
  180.                     while(!infile.eof())
  181.                     {
  182.                         infile.read((char*)&obj,sizeof(obj));
  183.                         if(strcmp(str,obj.ownerofcar)!=0)
  184.                         {
  185.                             outfile.write((char*)&obj,sizeof(obj));
  186.                             found=true;
  187.                         }
  188.                         infile.read((char*)&obj,sizeof(obj));
  189.                     }
  190.                     if(!found)
  191.                         cout << "No items matched your search \n";
  192.                     infile.close();
  193.                     outfile.close();
  194.                     remove("car.txt");
  195.                     rename("temp.txt","car.txt");
  196.                     read();
  197.                     cout<<"Do you want to do anothe opertion (y\\n): \n";
  198.                     cin>>t;
  199.                     break;
  200.                 }
  201.         }
  202.     }while(t=='y');
  203.     cout<<"**THE END**";
  204.     return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement