Output to CSV problem

Jul 20, 2013 at 12:28am
I'm trying to write a code which populates a structure and outputs its values to CSV. Currently it only outputs the first row defining the values, but nothing else. The other functions called in main() just populate the structure based on a line inputted from a text file.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  struct AlarmLine
{
	int linetype;
	//string IDnum;
	string ptype;
	int day, month, year;
	int hour, minute, second;
	string failtype;
	int counter;
	int duration;
	char a,b,c,d,e,f;
};

int CSV_Output (AlarmLine Failure, string line)
{
	AlarmLine FailureLine;
	FailureLine = Failure;
	string outpath;
	outpath = "C:\\Users\\user\\Documents\\output.csv";
	ofstream out( outpath.c_str() );
	out << FailureLine.month << "," << FailureLine.day << "," << FailureLine.year << ",";
	out << FailureLine.hour << ":" << FailureLine.minute << ",";
	out << FailureLine.ptype.c_str() << "," << FailureLine.failtype.c_str() << ",";
	out << FailureLine.a << ",";
	if(FailureLine.b != ' '){
		out << FailureLine.b << ",";
		if(FailureLine.c != ' '){
			out << FailureLine.c << ",";
			if(FailureLine.d != ' '){
				out << FailureLine.d << ",";
				if(FailureLine.e != ' '){
					out << FailureLine.e << ",";
					if(FailureLine.f != ' '){
						out << FailureLine.f << ",";
					}
				}
			}
		}
	}
	if(FailureLine.linetype == 1){
		out << FailureLine.duration;
	}
	out << endl;
	return 1;
}

int main ()
{
	int h = 0;
	string inpath, outpath, line;
	AlarmLine FailureLine;
	FailureLine.counter = 0;
	inpath = "C:\\Users\\user\\Documents\\inputs\\";
	do
	{
		line = GetNextLine(FailureLine, inpath);
		FailureLine.counter++;
		FailureLine.linetype = FindLineType(FailureLine, line);
		FailureLine = Findptype(FailureLine, line);
		FailureLine = FindID(FailureLine, line);
		FailureLine = FindPriority(FailureLine, line);
		FailureLine = FindTime(FailureLine, line);
		FailureLine = FindFailDuration(FailureLine, inpath);
		string outpath;
		outpath = "C:\\Users\\user\\Documents\\SCADA Logs\\Outputs\\SCADASalinas2012.csv";
		ofstream out( outpath.c_str() );
		out << "Month" << "," << "Day" << "," << "Year" << "," << "Time" << ","; //output titles of table columns
		out << "part type" << "," << "failure type" << "," << "ID Number" << "," << "Failure Duration" << endl;
		if((FailureLine.linetype != 0) && (line != "EOF")) 
		{	
			CSV_Output (FailureLine, line); //Output the CSV table values from the 
		}
	}
	while(FailureLine.linetype != -2);
	return 1;
}


Thanks in advance for any help guys, I'm counting on you!!!
Jul 20, 2013 at 3:03am
The file is being wiped each time you open it. You need to open it for append.
Jul 20, 2013 at 3:56am
ofstream file("filename.txt", std::ios_base::app);
Jul 20, 2013 at 7:06am
Or change the logic, move the code which opens the output file and outputs headings, before the start of the do loop.
Topic archived. No new replies allowed.