input and output file

I wanna to write results of some calculation into a text file and then at the end, read them from text file and do some other calculation. But my code has an error in the line that i wanna to read text file, the error is for getline. This is a section of my code. data is an array that i read from another text file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ofstream output1;

output1.open("output1.txt");

for (int sam5 = 2; sam5 < NrSamples; sam5++) {
					
 double a = 1 / t;
 double b = exp((-T) / t);
 Data[0] = 0;
 Data[1] = 0;
						
 Data[sam5] = (2 * b * Data[sam5 - 1]) - (pow(b,2)*Data[sam5 - 2]) + (stoi(data[sam5])) -( b*(1 + a*T)*(stoi(data[sam5 - 1])));
 output1 << NUM++ << '\t' << Data[sam5] << endl;
						
 }

if (output1.is_open()) {
	while (output1) {
							     output1.getline(DATA[index], 5);

						}
					}
The problem is that you cannot read from an output stream (ofstream).

(It’s okay. Don’t slap yourself too hard.)

Either close the text file or flush it to make sure that all the data has been written. Then open it with an ifstream and do your reading.

Hope this helps.
Topic archived. No new replies allowed.