Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string.h>
- using namespace std;
- class car
- {
- public:
- int id,model;
- char name[10],ownerofcar[10];
- };
- void read(){
- car obj2;
- ifstream infile;
- infile.open("car.txt",ios::in);
- if(infile.is_open())
- {
- int id=0;
- while(! infile.eof())
- {
- infile.read((char*)&obj2,sizeof(obj2));
- if(obj2.id != id)
- {
- cout<< obj2.id << "\t" << obj2.model << "\t" << obj2.name << "\t" <<obj2.ownerofcar <<endl;
- id = obj2.id;
- }
- }
- }
- else cout<< "Can't open the file";
- infile.close();
- }
- int main()
- {
- char t;
- cout<<"**Welcome**\n";
- do{
- cout<<"Enter the number of opertion you want\n";
- cout<<"1-Write on the file\n";
- cout<<"2-Read from the file\n";
- cout<<"3-search on the file\n";
- cout<<"4-Copy the file\n";
- cout<<"5-length of the file\n";
- cout<<"6-update on the file\n";
- cout<<"7-Delete from the file\n";
- int s;
- cin>>s;
- switch(s){
- case 1:
- {
- car obj1;
- char c;
- ofstream outfile(("car.txt"));
- do
- {
- cout<< "Enter Car ID: ";
- cin>>obj1.id;
- cout<< "Enter Car Model: ";
- cin>>obj1.model;
- cout<< "Enter Car Name: ";
- cin>>obj1.name;
- cout<< "Enter Owner Of The Car: ";
- cin>>obj1.ownerofcar;
- outfile.write((char*)&obj1,sizeof(obj1));
- cout<< "Enter another record? (y/n) ";
- cin>>c;
- }
- while(c=='y');
- outfile.close();
- cout<<"Do you want to do anothe opertion (y\\n): \n";
- cin>>t;
- break;
- }
- case 2:
- {
- read();
- cout<<"Do you want to do anothe opertion (y\\n): \n";
- cin>>t;
- break;
- }
- case 3:
- {
- car obj;
- bool found=false;
- char str[10];
- cout<< "Enter owner name to search for: ";
- cin>>str;
- ifstream infile("car.txt");
- while(!infile.eof())
- {
- infile.read((char*)&obj,sizeof(obj));
- if(strcmp(str,obj.ownerofcar)==0)
- {
- found = true;
- cout<< "ID" << "\t"<< "Model" << "\t"<< "Name" << "\t"<< "Owner Of Car\n";
- cout << "----------------------------------" << endl;
- cout << obj.id << "\t" << obj.model << "\t"<< obj.name << "\t"<<obj.ownerofcar <<endl;
- break;
- }
- }
- if(!found)
- cout << "No items matched your search \n";
- infile.close();
- cout<<"Do you want to do anothe opertion (y\\n): \n";
- cin>>t;
- break;
- }
- case 4:
- {
- char ch;
- ifstream infile("Car.txt");
- ofstream outfile("copied file.txt");
- while(infile.get(ch))
- {
- outfile.put(ch);
- }
- infile.close();
- cout<<"Do you want to do anothe opertion (y\\n): \n";
- cin>>t;
- break;
- }
- case 5:
- {
- int ch1;
- ifstream infile("Car.txt");
- infile.seekg(0,ios::end);
- cout<<" length of the file:"<<infile.tellg()<<endl;
- infile.close();
- cout<<"Do you want to do anothe opertion (y\\n): \n";
- cin>>t;
- break;
- }
- case 6:
- {
- car obj;
- bool found=false;
- char str[10];
- cout<< "Enter owner name to search for: ";
- cin>>str;
- fstream infile("car.txt",ios::in|ios::out);
- infile.read((char*)&obj,sizeof(obj));
- while(!infile.eof())
- {
- if(strcmp(str,obj.ownerofcar)==0)
- {
- cout<<"Enter the new model for "<<str<<" ";
- cin>>obj.model;
- int curpos=infile.tellg();
- int namesize=sizeof(str);
- infile.seekp(curpos-namesize,ios::beg);
- infile.write((char*)&obj,sizeof(obj));
- found = true;
- //infile.seekg(curpos-namesize,ios::beg);
- //infile.read((char*)&obj,sizeof(obj));
- cout<< "ID" << "\t"<< "Model" << "\t"<< "Name" << "\t"<< "Owner Of Car\n";
- cout << "----------------------------------" << endl;
- cout << obj.id << "\t" << obj.model << "\t"<< obj.name << "\t"<<obj.ownerofcar <<endl;
- break;
- }
- //infile.read((char*)&obj,sizeof(obj));
- }
- if(!found)
- cout << "No items matched your search \n";
- infile.close();
- cout<<"Do you want to do anothe opertion (y\\n): \n";
- cin>>t;
- break;
- }
- case 7:
- {
- car obj;
- bool found=false;
- char str[10];
- cout<< "Enter owner name to delete for: ";
- cin>>str;
- ifstream infile("car.txt",ios::in);
- ofstream outfile("temp.txt",ios::out);
- while(!infile.eof())
- {
- infile.read((char*)&obj,sizeof(obj));
- if(strcmp(str,obj.ownerofcar)!=0)
- {
- outfile.write((char*)&obj,sizeof(obj));
- found=true;
- }
- infile.read((char*)&obj,sizeof(obj));
- }
- if(!found)
- cout << "No items matched your search \n";
- infile.close();
- outfile.close();
- remove("car.txt");
- rename("temp.txt","car.txt");
- read();
- cout<<"Do you want to do anothe opertion (y\\n): \n";
- cin>>t;
- break;
- }
- }
- }while(t=='y');
- cout<<"**THE END**";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement