File Input/Output

Hello,

My program reads in 3 numbers from a resource file name "data2.txt"

data2.txt:
-------------
3
4
5
-------------

and attempts to read the numbers of this file into an empty file named "results.txt"

when it outputs and I check "results.txt" all that shows up is the "5", what can I add to my program to make it put 3, 4 & 5 into "results.txt".

Cheers

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
38
39
40
41
42
43
44
45
46
47
48
49
50
  #include <iostream>
#include <fstream>
using namespace std;

int main()
{
	int dataNums;

	fstream dataFile;
	fstream resultsFile;
	//---------------------------------------------------------------------
	dataFile.open("data2.txt", ios::in);

	if (!dataFile)
	{
		cout << "\"data2.txt\" did not open. Now closing. \n";
		exit(1);
	}

	while (!dataFile.eof())
	{
		dataFile >> dataNums;
	}
	//---------------------------------------------------------------------
	resultsFile.open("results.txt", ios::out);

	if (!resultsFile)
	{
		cout << "\"results.txt\" did not open. Now closing. \n";
		exit(1);
	}

	resultsFile << dataNums;

	cout << "The data has been written to \"results.txt\".";
	cout << endl << endl;
	//---------------------------------------------------------------------

	dataFile.close();
	resultsFile.close();

	return 0;
}

/*
Output:
The data has been written to "results.txt".

Press any key to continue . . .
*/
look at arrays. you are only storing the last value in the variable
so changing int dataNums into an array would work?
yes
Topic archived. No new replies allowed.