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 p4;
- 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 *)&p4, sizeof(p4));
- if (strcmp(str, p4.getName()) == 0)
- {
- cout << "Enter the new price for " << str << " ";
- p4.setPrice();
- int curpos = f.tellg();
- int stusize = sizeof(p4);
- f.seekp(curpos - stusize, ios::beg);
- f.write((char *)&p4, sizeof(p4));
- found = true;
- break;
- }
- }
- f.close();
- if (!found)
- cout << "Product name does not exist.\n";
- }
- }
- void deleterecord()
- {
- fstream in("product.txt", ios::in);
- ofstream out("Product Temp.txt", ios::out);
- if (in.is_open())
- {
- Product p5;
- char str[20];
- int num;
- cout << "Enter the id of product to delete his record ";
- cin >> num;
- while (in.read((char *)&p5, sizeof(p5)))
- {
- if (num!=p5.getId())
- {
- 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();
- deleterecord();
- //readproduct();
- //updateproduct();
- readproduct();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement