Below is my code in C++ about updating and deleting particular Product from a text file. It really works but after functioning, it create the things in the command box picture. Parallel to that, text file appears to create space according to product view in command box picture. The two functions are mostly the same.
void delete_Product(struct Product p)
{
fstream exa;
fstream temp;
char code[25];
exa.open("example.txt", ios::in);
temp.open("temp.txt", ios::out);
cin.ignore();
cout<<"\nEnter Product Code : ";
cin.getline(code,25);
while (!exa.eof())
{
exa.getline(p.P_code,25,'\n');
exa.getline(p.P_name,25,'\n');
exa.getline(p.meg,25,'\n');
exa.getline(p.expiry,25,'\n');
exa.getline(p.producer,25);
search_Product(p,code);
if (strcmp(p.P_code,code)==0)
{
continue;
} else
{
temp<<p.P_code<<'\n'<<p.P_name<<'\n'<<p.meg<<'\n'<<p.expiry<<'\n'<<p.producer<<'\n';
}
}
temp.close();
exa.close();
exa.open("example.txt", ios::out);
temp.open("temp.txt", ios::in);
while (!temp.eof())
{
temp.getline(p.P_code,25,'\n');
temp.getline(p.P_name,25,'\n');
temp.getline(p.meg,25,'\n');
temp.getline(p.expiry,25,'\n');
temp.getline(p.producer,25);
exa<<p.P_code<<'\n'<<p.P_name<<'\n'<<p.meg<<'\n'<<p.expiry<<'\n'<<p.producer<<'\n';
}
temp.close();
exa.close();
remove("temp.txt");
cout<<"Done!";
}
void update_Product(struct Product p)
{
fstream exa;
fstream temp;
char code[25];
exa.open("example.txt", ios::in);
temp.open("temp.txt", ios::out);
cin.ignore();
cout<<"\nEnter Product Code : ";
cin.getline(code,25);
while (!exa.eof())
{
exa.getline(p.P_code,25,'\n');
exa.getline(p.P_name,25,'\n');
exa.getline(p.meg,25,'\n');
exa.getline(p.expiry,25,'\n');
exa.getline(p.producer,25);
search_Product(p,code);
if (strcmp(p.P_code,code)==0)
{
cout<<"\n\nPlease update the selected Product";
cout<<"\nEnter Product's ID: ";
cin>>p.P_code;
cin.ignore();
cout<<"Enter Product's Name: ";
cin.getline(p.P_name, 25);
cout<<"Enter Manufacturing date: ";
cin>>p.meg;
cout<<"Enter Expiry date: ";
cin>>p.expiry;
cin.ignore();
cout<<"Enter Producer's Name: ";
cin.getline(p.producer, 25);
temp<<p.P_code<<'\n'<<p.P_name<<'\n'<<p.meg<<'\n'<<p.expiry<<'\n'<<p.producer<<'\n';
}
else
{
temp<<p.P_code<<'\n'<<p.P_name<<'\n'<<p.meg<<'\n'<<p.expiry<<'\n'<<p.producer<<'\n';
}
}
temp.close();
exa.close();
exa.open("example.txt", ios::out);
temp.open("temp.txt", ios::in);
while (!temp.eof())
{
temp.getline(p.P_code,25,'\n');
temp.getline(p.P_name,25,'\n');
temp.getline(p.meg,25,'\n');
temp.getline(p.expiry,25,'\n');
temp.getline(p.producer,25);
exa<<p.P_code<<'\n'<<p.P_name<<'\n'<<p.meg<<'\n'<<p.expiry<<'\n'<<p.producer<<'\n';
}
temp.close();
exa.close();
remove("temp.txt");
cout<<"Done!";
}
My full code is on Google Drive. Below are screenshots of the output: