storing data in C++

I am running an exe file with C++. Now for each run I have one output file. Altogether I will have 100 runs. I want after each run the output should store in one file (say, "final_output.txt") under different columns. like, the output of the first run in col1, output of second run in col2 and so on....

i am not getting any clue to proceed on this....any suggestion!!!

1) Use some kind of container to hold the information. IE you could have a vector<Data>.. where 'Data' is a struct you create which contains all of a single run's output.

2) Do a run

3) Read the run's output into a 'Data' object

4) push_back that object into your container

5) Repeat from step #2 until all runs complete

6) Now that you have all the output data in a single vector, it should be pretty straightforward to dump it all to a final output file.
I have made the program like 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
string line;
		vector<string> vec;
		vector<double> cpool;
		int num_cols;
		int cntr = 0;
		
		double sum = 0.0;

		double litter = 0.0;
		double Corg = 0.0;
		double bio = 0.0;

		
		vector<string>vec_n;
		string line_n;
		vector<string>vec_nit;
		string line_nit;
		vector<string>vec_soiln;
		string line_soiln;
		
		ifstream dataread2("dc_sip.csv");
		ifstream datareadnit("summary.out");
		ifstream dataread3("soiln.out");

		stringstream out12, out13, out14, out19, out20, out26, out27, out28;
		out12<<"output"<<"/"<<"NPP.out";
		out13<<"output"<<"/"<<"NEE.out";
		out14<<"output"<<"/"<<"C_soil.out";
		out19<<"output"<<"/"<<"NO3.out";
		out20<<"output"<<"/"<<"NH4.out";
		out26<<"output"<<"/"<<"BIO_AG.out";
		out27<<"output"<<"/"<<"Year_N2O.out";
		out28<<"output"<<"/"<<"N2O.out";

		ofstream dataout12(out12.str(),ios::app);
		ofstream dataout13(out13.str(),ios::app);
		ofstream dataout14(out14.str(),ios::app);
		ofstream dataout19(out19.str(),ios::app);
		ofstream dataout20(out20.str(),ios::app);
		ofstream dataout26(out26.str(),ios::app);
		ofstream dataout27(out27.str(),ios::app);
		ofstream dataout28(out28.str(),ios::app);

		dataout12 << ID[a] << "\t"; 		// NPP
		dataout13 << ID[a] << "\t"; 		// NEE
		dataout14 << ID[a] << "\t"; 		// C_ORG_SOIL
		dataout19 << ID[a] << "\t"; 		// NO3
		dataout20 << ID[a] << "\t"; 		// NH4
		dataout26 << ID[a] << "\t";  		// BIO AG
		dataout27 << ID[a] << "\t";  		// year_summary_N2O
		dataout28 << ID[a] << "\t";  		// N2O

		

		while(getline(dataread2,line))
		{
			if(line.size() == 0)
				break;
			vec = readline(line);

			while(getline(datareadnit,line_nit))
			{
				if(line_nit.size() == 0)
					break;
				vec_nit = readline(line_nit);

				while(getline(dataread3,line_soiln))
				{
					if(line_soiln.size() == 0)
						break;
					vec_soiln = readline(line_soiln);

					if((str2double(vec[0])>=year1) && (str2double(vec[0])<year2 + 1))
					{
						Corg = str2double(vec[51]) + str2double(vec[52]) + str2double(vec[53]) + str2double(vec[54]) + str2double(vec[55]);
						bio = str2double(vec[33]) + str2double(vec[34]) + str2double(vec[35]);
  						dataout14 << Corg << "\t"; 		// C_ORG_SOIL
						dataout12 << vec[31] << "\t"; 		// NPP
  						dataout13 << vec[32]<< "\t"; 		// NEE
						dataout26 << vec[33] << "\t";
						dataout27 << str2double(vec[34]) + str2double(vec[35]) << "\t";
						dataout28 << vec_nit[5] << "\t";
						dataout19 << ((2*str2float(vec_soiln[3])) + (3*str2float(vec_soiln[4])) + (5*str2float(vec_soiln[5])) + (10*str2float(vec_soiln[6])))/20 << "\t";
						dataout20 << vec_soiln[2] << "\t";
					}
				}
			}
		}

		dataout14 << "\n"; 		// C_ORG_SOIL
		dataout19 << "\n"; 		// NO3
		dataout20 << "\n"; 		// NH4
		dataout12 << "\n";  		// NPP
		dataout13 << "\n";  		// NEE
		dataout26 << "\n";
		dataout27 << "\n";
		dataout28 << "\n";


it is giving error!!!
it is giving error!!!


The thing about error messages is they not only tell you what the problem is, but they also tell you exactly what line the problem is on.

So how about posting the error message here so we can see where and what the problem is?
sorry!! this the entire program:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include <iomanip>
#include<direct.h>
#include<windows.h>
#include<vector>
#include <cstdlib>
#include<math.h>
#include<stdio.h>

using namespace std;

double str2double ( string str );
float str2float ( string str );
vector<string> readline(string str);

void main()
{
	int ID[5];
	int year1, year2;

	ifstream num("number.txt");

	for(int a=0; a<4; a++)
	{
		while(num>>ID[a])
		{

			
		
		 
		stringstream sch;
		sch<<"schedule"<<"/"<<"schdule_"<<ID[a]<<".txt";
		string schedule = sch.str();

		stringstream sit;
		sit<<"Site"<<"/"<<"Site"<<ID[a]<<".txt";
		string site = sit.str();

		stringstream sol;
		sol<<"soils"<<"/"<<"soilfile_"<<ID[a]<<".txt";
		string soil = sol.str();

		stringstream wet;
		wet<<"weather"<<"/"<<"climate_CX_"<<ID[a]<<".txt";
		string weather = wet.str();
	
		ifstream sch_in(schedule);
		ifstream site_in(site);
		ifstream soil_in(soil);
		ifstream weather_in(weather);
		ofstream example("example.sch");
		ofstream site_out("site.100");
		ofstream soil_out("soils.in");
		ofstream weather_out("weather.wth");

		string line, line1, line2, line3;
	
		while(getline(sch_in,line))
		{
			example<<line<<endl;
		}
	
		while(getline(site_in, line1))
		{
			site_out<<line1<<endl;
		}

		while(getline(soil_in, line2))
		{
			soil_out<<line2<<endl;
		}
		while(getline(weather_in, line3))
		{
			weather_out<<line3<<endl;
		}

		cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
		cout<<"++++++++++++++++++++++++++++++++NOW RUNNING DAILYDAYCENT WITH THE FILE TYPE  "<<ID[a]<<"+++++++++++++++++++++++++++++++++++++"<<endl;
		cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
	
		stringstream dd;
		dd<<"dailydaycent"<<" -s "<<"example"<<" -n "<<"example";
		string daycent = dd.str();

		system(daycent.c_str());

		remove("example.bin");
		example.close();
		site_out.close();
		soil_out.close();
		weather_out.close();
		sch_in.close();
		site_in.close();
		soil_in.close();
		weather_in.close();
		
		//data saving part!!

		
				
		double sum = 0.0;
		double litter = 0.0;
		double Corg = 0.0;
		double bio = 0.0;

		string line;
		vector<string>vec;
		string line_n;
		vector<string>vec_n;
		string line_nit;
		vector<string>vec_nit;
		string line_soiln;
		vector<string>vec_soiln;
		
		
		ifstream dataread2("dc_sip.csv");
		ifstream datareadnit("summary.out");
		ifstream dataread3("soiln.out");

		stringstream out12, out13, out14, out19, out20, out26, out27, out28;
		out12<<"output"<<"/"<<"NPP.out";
		out13<<"output"<<"/"<<"NEE.out";
		out14<<"output"<<"/"<<"C_soil.out";
		out19<<"output"<<"/"<<"NO3.out";
		out20<<"output"<<"/"<<"NH4.out";
		out26<<"output"<<"/"<<"BIO_AG.out";
		out27<<"output"<<"/"<<"Year_N2O.out";
		out28<<"output"<<"/"<<"N2O.out";

		ofstream dataout12(out12.str(),ios::app);
		ofstream dataout13(out13.str(),ios::app);
		ofstream dataout14(out14.str(),ios::app);
		ofstream dataout19(out19.str(),ios::app);
		ofstream dataout20(out20.str(),ios::app);
		ofstream dataout26(out26.str(),ios::app);
		ofstream dataout27(out27.str(),ios::app);
		ofstream dataout28(out28.str(),ios::app);

		dataout12 << ID[a] << "\t"; 		// NPP
		dataout13 << ID[a] << "\t"; 		// NEE
		dataout14 << ID[a] << "\t"; 		// C_ORG_SOIL
		dataout19 << ID[a] << "\t"; 		// NO3
		dataout20 << ID[a] << "\t"; 		// NH4
		dataout26 << ID[a] << "\t";  		// BIO AG
		dataout27 << ID[a] << "\t";  		// year_summary_N2O
		dataout28 << ID[a] << "\t";  		// N2O

		

		while(getline(dataread2,line))
		{
			if(line.size() == 0)
				break;
			vec = readline(line);

			while(getline(datareadnit,line_nit))
			{
				if(line_nit.size() == 0)
					break;
				vec_nit = readline(line_nit);

				while(getline(dataread3,line_soiln))
				{
					if(line_soiln.size() == 0)
						break;
					vec_soiln = readline(line_soiln);

					if((str2double(vec[0])>=year1) && (str2double(vec[0])<year2 + 1))
					{
						Corg = str2double(vec[51]) + str2double(vec[52]) + str2double(vec[53]) + str2double(vec[54]) + str2double(vec[55]);
						bio = str2double(vec[33]) + str2double(vec[34]) + str2double(vec[35]);
  						dataout14 << Corg << "\t"; 		// C_ORG_SOIL
						dataout12 << vec[31] << "\t"; 		// NPP
  						dataout13 << vec[32]<< "\t"; 		// NEE
						dataout26 << vec[33] << "\t";
						dataout27 << str2double(vec[34]) + str2double(vec[35]) << "\t";
						dataout28 << vec_nit[5] << "\t";
						dataout19 << ((2*str2float(vec_soiln[3])) + (3*str2float(vec_soiln[4])) + (5*str2float(vec_soiln[5])) + (10*str2float(vec_soiln[6])))/20 << "\t";
						dataout20 << vec_soiln[2] << "\t";
					}
				}
			}
		}

		dataout14 << "\n"; 		// C_ORG_SOIL
		dataout19 << "\n"; 		// NO3
		dataout20 << "\n"; 		// NH4
		dataout12 << "\n";  		// NPP
		dataout13 << "\n";  		// NEE
		dataout26 << "\n";
		dataout27 << "\n";
		dataout28 << "\n";
			
  
		}
		
	}
	
	

	system("pause");

}

//..........................................................................................................................................................

double str2double(string str)
{
	// string to double
	istringstream buffer(str);
	double in;
	buffer >> in;
	return in;
}

//...........................................................................................................................................................

float str2float ( string str )
{
	// string to double
	istringstream buffer(str);
	float  in;
	buffer >> in;
	return in;
}
//............................................................................................................................................................
vector<string> readline(string str)
{
     vector<string>vec_str;
     string str2; 
     int len = str.size();
     double val;
     size_t offset;
     int cnt = 0;
     int ende, start = 0;

     while(((offset = str.find(",")) != -1) || ((offset = str.find(" ")) != -1) || ((offset = str.find("  ")) != -1) || ((offset = str.find("   ")) != -1) || ((offset = str.find("\t")) != -1))
     {
                   cnt++;
                   if(offset == len)
                          break;
                   ende = offset - 1;
                   str2 = str.substr(start,(ende-start+1));
                   if((str2.size() > 0) && (str2 != " "))
                            vec_str.push_back(str2);
                   start = ende+1;
                   str.erase(offset,1);
     }
     ende = str.size();
     str2 = str.substr(start,(ende-start+1));
     vec_str.push_back(str2);

     return vec_str;
}



the exe file is runnin gwithout any problem. Now when I am trying to take the output - this is the error msg:
1>c:\users\nhy690\documents\visual studio 2010\projects\test_1\test_1\test_1.cpp(109): error C2086: 'std::string line' : redefinition
1> c:\users\nhy690\documents\visual studio 2010\projects\test_1\test_1\test_1.cpp(59) : see declaration of 'line'
the exe file is runnin gwithout any problem. Now when I am trying to take the output - this is the error msg:

Um, if the compiler is giving you errors, then how can you possibly have an exe file to run?

Look at the two lines of code that are generating the error message. It should be pretty obvious what the problem is.

by the "exe file", i meant the the "exe file" that is used by the system command. the program is running well upto the "data saving part".

I could not understand the error msg - "std::string line' : redefinition"
I have solved the error "redefinition". But the program is not storing the results!!
Isn't it storing the results in NPP.out, NEE.out, etc?
no!! it is not storing the results in those files!!
Keep it simple. Output one column only. Use paste to merge the tables:
http://en.wikipedia.org/wiki/Paste_%28Unix%29
keskivetro: can you give a solution in C++??
No, but paste is a GNU app, so its (C) sources are open.
Topic archived. No new replies allowed.