Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <bits/stdc++.h>
- #include<string.h>
- #include<fstream>
- using namespace std;
- class Product
- {
- int id;
- int price;
- char name[20];
- public :
- void setId()
- {
- cout <<"Enter ID : ";
- cin >> id;
- }
- int getId()
- {
- return id;
- }
- void setPrice()
- {
- cout <<"Enter price : ";
- cin >> price;
- }
- int getPrice()
- {
- return price;
- }
- void setName()
- {
- cout << "Enter name : ";
- cin >> name ;
- }
- char* getName()
- {
- return name;
- }
- };
- void writeproduct()
- {
- fstream out("product.txt", ios::out | ios::app );
- Product p1 ;
- char ch;
- do
- {
- p1.setId();
- p1.setName();
- p1.setPrice();
- out.write( (char*)&p1, sizeof(p1));
- cout <<"if you want to enter another data\tpress Y/N \n";
- cin >> ch ;
- }
- while( ch=='Y' || ch=='y');
- out.close();
- }
- void readproduct()
- {
- Product p2;
- ifstream in;
- in.open ("product.txt", ios::in);
- if(in.is_open())
- {
- cout << "Id\tName\tPrice\n";
- while(!in.eof())
- {
- in.read( (char *) &p2, sizeof(p2));
- if(!in.eof())
- cout << p2.getId() << '\t' << p2.getName()<< '\t' << p2.getPrice() << "\n" ;
- }//in.close();
- }
- else
- {
- cout <<"Can't open the file \n";
- }
- }
- void searchproduct()
- {
- Product p3;//creating new third object
- char str[10];
- bool found=false;
- cout<<"Enter name to search for :";
- cin>>str;
- fstream in2;//check this
- in2.open("product.txt",ios::in);//check this
- if(in2.is_open())
- {
- in2.read((char*)&p3,sizeof(p3));//read
- while(!in2.eof()) //check
- {
- if(strcmp(str,p3.getName())==0)
- {
- cout << p3.getId() << '\t' << p3.getName()<< '\t' << p3.getPrice() << "\n" ;//print
- found=true;
- }
- in2.read((char*)&p3,sizeof(p3));//read
- }
- if(!found)cout<<"Name not found\n";
- in2.close();
- }
- else cout<<"Cannot open this file"<<"\n";
- }
- void updateproduct()
- {
- Product p;
- fstream f("product.txt",ios::in | ios::out);
- if(f.is_open())
- {
- char str[20];
- cout<<"Enter the product name to update his record ";
- cin>>str;
- bool found=false;
- while(!f.eof())
- {
- f.read((char*)&p,sizeof(p));
- if(strcmp(str,p.getName())==0)
- {
- cout<<"Enter the new price for "<<str<<" ";
- p.setPrice();
- int curpos=f.tellg();
- int stusize=sizeof(p);
- f.seekp(curpos-stusize,ios::beg);
- f.write((char*)&p,sizeof(p));
- found=true;
- break;
- }
- }
- f.close();
- if(!found)
- cout<<"Product name does not exist.";
- }
- }
- void delet()
- {
- fstream in("product.txt",ios::in);
- ofstream out("Product Temp.txt",ios::out);
- Product p5;
- char str[20];
- cout<<"Enter the name of product to delete his record ";
- cin>>str;
- if(in.is_open())
- {
- while(in.read((char*)&p5,sizeof(p5)))
- {
- if(strcmp(str,p5.getName())!=0)
- {
- out.write((char*)&p5,sizeof(p5));
- }
- }
- in.close();
- out.close();
- remove("product.txt");
- rename("Product Temp.txt","product.txt");
- }
- }
- int main()
- {
- // writeproduct();
- readproduct();
- // updateproduct();
- //readproduct();
- //searchproduct();
- //readproduct();
- //delet();
- //readproduct();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement