#include <fstream>
#include <iostream>
#include <string>
usingnamespace std;
void inputLine(ofstream&);
void getNum(int&, string);
int main()
{
ofstream fsOut;
int numLines = 0;
int i = 0;
getNum(numLines, "lines");
for (i = 0; i <= numLines; i++)
{
inputLine(fsOut);
}
return 0;
}
void inputLine(ofstream& fsTxt)
{
char str[100];
fsTxt.open("data.txt");
cin.getline(str, sizeof(str));
fsTxt << str << endl;
fsTxt.close();
}
void getNum(int& num, string pluralType)
{
cout << "Enter number of " << pluralType << ": ";
cin >> num;
}
This program only puts one of the lines (the last one) that a user types into a file called "data.txt". I think this is because the old data.txt gets overwritten each time a new line is entered. How do I open an existing file and write to it without overwriting the file?