C++ Data File Handling Project with Delete, Modify and Restore Deleted data(with Classes and Objects)
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class project{
int pid;
char pname[20];
char cate[20];
int qty;
float rate;
float amt;
public:
char* getpname();
int getid();
void menu();
void addrecord();
void getdata();
void readrecord();
void showdata();
void search();
void deleterecord();
void restoredeletedrecord();
void viewdeletedrecord();
void modifyrecord();
};
project p;
char* project::getpname()
{
return pname;
}
int project::getid()
{
return pid;
}
void project::showdata()
{
cout<<"Product ID : "<<pid<<endl;
cout<<"Product Name : "<<pname<<endl;
cout<<"Product Category: "<<cate<<endl;
cout<<"Product Rate : "<<rate<<endl;
cout<<"Product Quantity: "<<qty<<endl;
cout<<"Product Amount : "<<amt<<endl;
cout<<"================================\n";
}
void project::menu()
{
int ch;
do{
cout<<"========================\n";
cout<<"|PROJECT 1 With DFH|\n";
cout<<"========================\n";
cout<<"0.Exit\n";
cout<<"1.Add A Product\n";
cout<<"2.Display All Products\n";
cout<<"3.Search A Products\n";
cout<<"4.Delete A Product\n";
cout<<"5.Review Deleted Product\n";
cout<<"6.Modify A Product\n";
cout<<"Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
system("cls");
addrecord();
system("pause");
system("cls");
break;
case 2:
system("cls");
readrecord();
system("pause");
system("cls");
break;
case 3:
system("cls");
search();
system("pause");
system("cls");
break;
case 4:
system("cls");
deleterecord();
system("pause");
system("cls");
break;
case 5:
system("cls");
viewdeletedrecord();
system("pause");
system("cls");
break;
case 6:
system("cls");
modifyrecord();
// system("pause");
system("cls");
break;
}
}while(ch!=0);
}
void project::viewdeletedrecord()
{
int id;
ifstream fin;
ofstream fout;
fin.open("trash.hit",ios::binary);
fout.open("products.hit",ios::binary|ios::app);
cout<<"This is what I Found in Trash...\n";
while(fin.read((char*)&p,sizeof(p)))
{
showdata();
}
fin.close();
fin.open("trash.hit",ios::binary);
cout<<"Enter the Product Id to restore : ";
cin>>id;
int flag=0;
while(fin.read((char*)&p,sizeof(p)))
{
if(id==getid())
{
cout<<"Following product has been shifted...\n";
showdata();
fout.write((char*)&p,sizeof(p));
flag++;
}
}
if(flag==0)
cout<<"No such "<<id<<" in the file to restore....\n";
fin.close();
fout.close();
}
void project::deleterecord()
{
int chh,id;
ofstream fout1,fout2;
fout1.open("tmp.hit");
fout2.open("trash.hit",ios::app);
ifstream fin;
fin.open("products.hit",ios::binary);
cout<<"Enter the Id to Delete:";
cin>>id;
while(fin.read((char*)&p,sizeof(p)))
{
if(id==getid())
{
showdata();
cout<<"Following Record Will Be Deleted...\n";
fout2.write((char*)&p,sizeof(p));
}
else
{
fout1.write((char*)&p,sizeof(p));
}
}
fout1.close();
fout2.close();
fin.close();
remove("products.hit");
rename("tmp.hit","products.hit");
}
void project::getdata()
{
cout<<"Enter the Product id : ";
cin>>pid;
cout<<"Enter the Product Name : ";
cin>>pname;
cout<<"Enter the Product Category : ";
cin>>cate;
cout<<"Enter the Product Rate : ";
cin>>rate;
cout<<"Enter the Product Quantity : ";
cin>>qty;
amt=qty*rate;
}
void project::modifyrecord()
{
fstream fio;
fio.open("products.hit",ios::in|ios::binary|ios::out);
int rollno,flag=0,pos;
cout<<"Enter the roll No To be Modified :";
cin>>rollno;
while(fio.read((char*)&p,sizeof(p)))
{
if(rollno==p.getid())
{
pos=fio.tellg();
cout<<"Following Record Will Be Modified...\n";
showdata();
cout<<"Enter New Details :";
fio.tellg();
}
else
{
cout<<"Not Found";
}
}
}
void project::addrecord()
{
ofstream fout;
fout.open("products.hit",ios::binary|ios::app);
getdata();
fout.write((char*)&p,sizeof(p));
fout.close();
cout<<"\nData Saved to File.....\n";
}
void project::search()
{
int chh,id;
ifstream fin;
fin.open("products.hit",ios::binary);
cout<<"Enter how You want to search \n";
cout<<"1. Search By Product id \n";
cout<<"2. Search By Product name \n";
cin>>chh;
switch(chh)
{
case 1:
cout<<"Enter the Id That you have to search:";
cin>>id;
while(fin.read((char*)&p,sizeof(p)))
{
if(id==getid())
{
showdata();
}
else
{
cout<<"id not found....\n";
}
}
break;
case 2:
char name[20];
cout<<"Enter the Name you have to search:";
cin.ignore();
cin.getline(name,20);
while(fin.read((char*)&p,sizeof(p)))
{
if(strcmpi(getpname(),name)==0)
{
showdata();
}
else
{
cout<<"id not found....\n";
}
}
break;
}
}
void project::readrecord()
{
ifstream fin;
fin.open("products.hit",ios::binary);
while(fin.read((char*)&p,sizeof(p)))
{
showdata();
}
fin.close();
cout<<"\nFile Reading Completed...\n";
}
int main()
{
p.menu();
}
Comments
Post a Comment