on writing to files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <fstream>
#include <iostream>
#include <string>

using namespace 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?
 
fsTxt.open("data.txt", ios_base::app);
Topic archived. No new replies allowed.