I am trying to make a simple program that can continously add data to a .txt document. Take a look at my code:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream playersW("players.txt");
ifstream playersR("players.txt");
int id;
string name, info = "";
float money;
while (playersR >> id >> name >> money) {
if (playersR.is_open()) {
info += to_string(id) + " " + name + " " + to_string(money) + "\n";
}
}
playersW << info;
playersR.close();
cout << "Enter player ID, Name and Money (or press 'CTRL+Z' + 'Enter' to quit):" << endl;
while (cin >> id >> name >> money) {
if (playersW.is_open()) {
playersW << id << " " << name << " " << money << endl;
}
}
playersW.close();
}
What I want is the program to first read the data that is stored in players.txt and then write it again and also add the new additional data to players.txt.
EDIT: With the code I have now, my program only writes in the file players.txt the new information that the user enters.