Hello there, I need to write a function that insert new data into a text file, and I manage to do that, but only for the first row....
When I close my program, and reopen it to key in a 2nd input, it will simplely overwrite my 1st input.
how can I insert the 2nd input at the next row of the 1st input?
here is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream fout;
string input;
string title,author,category;
float price;
fout.open("library.txt");
if (!fout.good())
{
cerr << "Error creating file";
}
cout << "Title: " ;
getline(cin, input);
title = input;
cout << "Author: " ;
getline(cin, input);
author = input;
cout << "Category: ";
getline(cin, input);
category = input;
cout << "Price: " ;
getline(cin, input);
istringstream strPrice(input);
strPrice >> price;
fout<< title << '\t' << author << '\t' << category << '\t' << price << "\n";
fout.close();
}
Many thanks~