Hello there, I met a problem which is to update inputs in a text file, but every time I update is only insert the data, not update.
May anyone give me some advises? Thanks~
string input;
ifstream fin;
ofstream fout;
string title,author,category;
float price;
fin.open("library.txt");
cout << "******Update Book******"<<endl;
cout << "Title: " ;
getline(cin, input);
fout.open("library.txt", ios::app | ios::out);
while(!fin.eof())
{
fin >> title >> author >> category >> price;
if (title == input)
{
cout << "1) Update Author" << endl;
cout << "2) Update Price" << endl;
cout << "Please input your subchoice: ";
getline(cin, input);
if(input == "1")
{
cout << "New Author : ";
getline(cin, input);
author = input;
fout<< title << '\t' << author << '\t' << category << '\t' << price ;
}
if(input == "2")
{
cout << "New Price : ";
getline(cin, input);
istringstream strPrice(input);
strPrice >> price;
fout<< title << '\t' << author << '\t' << category << '\t' << price ;
}
}
}