Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- class car
- {
- public:
- int id,model;
- char name[10],ownerofcar[10];
- };
- 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";
- int s;
- cin>>s;
- switch(s){
- case 1:
- car obj1;
- char c;
- ofstream outfile(("car.txt"));
- do
- {
- cout<< "Enter ID: ";
- cin>>obj1.id;
- cout<< "Enter Model: ";
- cin>>obj1.model;
- cout<< "Enter 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();
- break;
- case 2:
- 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();
- break;
- }
- }while(t=='y');
- cout<<"**THE END**";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement