Data Output

I am trying to read data from a file, test it, sort it, then write it to a file. I am able to sort the data but only the last lines that are sorted as bad or good by the program are written out. How do I fix this?

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
51
52
#include <iostream>
#include <fstream>
#include <string> 

using namespace std;

int main()
{
	double a,b,c,d,e,f; // aka variables
	
	void datatest(double data, double variables);
	string fail;
	double data;
	
	ifstream reader( "poem.txt" ) ;

if( ! reader )
{
cout << "Error opening input file" << endl ;
return -1 ;
}

	else
	{
	
	while (reader >> a >> b >> c >> d >> e >>f)
	{
		data = formula; // a formula used to test the data
		
	     datatest(data, variables);
	}
	}

	system("pause");
	return 0;
}

void datatest(double data, double variables) // sorts data
{ 
if (data == num)
 {
  ofstream output("file1.dat")
  output >> a >> b >> c >> d >> endl;
 }

 if (data != num)
 {
  ofstream output("file2.dat")
  output >> a >> b >> c >> d >> endl;
 }
 return;
}
I don't understand how lines 43 and 49 even compile in the first place - output streams use the shift left operator << and not the shift right operator >> like input streams do.

As for what you're experiencing, when you open a file stream it starts at the beginning, so you just overwrite the existing data. Use the ios::app flag when opening to start at the end.
Last edited on
Topic archived. No new replies allowed.