problem in reading file

I am trying to read a file (hwsd_soil_type_mainUK_11.txt) and store the output in different files (soils_id_a.in). The values of 'a' is from another file (temporary_11.txt) - this file has one column with 38 rows in it, these rows are the number of the output files. my code is preparing only the first file (soil_id_1.in). can anybody help me to solve the problem?? I have two functions which I have not included here!!
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
#include <fstream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <sstream>
#include <direct.h>
#include <string.h>
#include <math.h>


using namespace std;

vector <float> calc_PTF(int soil_indx);
int soil_type(float S,float U,float T);

void main()
{
	//for(int i = 1; i <=10; i++)
	//{
		stringstream in;
		in<<"hwsd_soil_type_mainUK_"<<11<<".txt";
		string inputfilename = in.str();

		stringstream temp;
		temp<<"temporary_"<<11<<".txt";
		string temporary = temp.str();

		stringstream dd;
		dd<<"mainUK_soilfile_"<<11;
		string dir=dd.str();
		mkdir(dir.c_str());
		  
			ifstream input(inputfilename);
		    ifstream tempo(temporary);			
		
			int Array[38];

		for(int a=0; a<=37; a++)
		{
			tempo>>Array[a];
			stringstream outfilename;
			outfilename<<dir<<"/soils_ID_"<<Array[a]<<".in";
			string out = outfilename.str();
			
            

		int ID, east, nort, n_soil,share, s_sand, s_silt, s_clay, sft;
		float t_sand, t_silt,t_clay, t_oc,t_bulk, t_ph, s_bulk, s_oc, s_ph;

		while(input>>ID>>east>>nort>>n_soil>>share>>t_sand>>t_silt>>t_clay>>t_bulk>>t_oc>>t_ph>>s_sand>>s_silt>>s_clay>>s_bulk>>s_oc>>s_ph>>sft)
		{
			
			  float S,U,T;
			  float swc_r, swc_s, WP, FC, sat_cond;
              float omat, pH, bd;
              int soil_indx;
              vector <float> soil_data;

			  S = t_sand;
			  U = t_silt;
			  T = t_clay;

			  soil_indx = soil_type(S,U,T);
		      soil_data = calc_PTF(soil_indx);

			  S= S/100;
			  T= T/100;
			  U= U/100;
		      pH = t_ph;
			  bd = t_bulk;
			  omat = (t_oc*1.72)/100;

			  swc_r = soil_data[0];
			  swc_s = soil_data[1];
			  WP = soil_data[2];
			  FC = soil_data[3];
			  sat_cond = soil_data[4];
			  float myints[] = {0.01,0.04,0.25,0.30,0.10,0.05,0.04,0.03,0.02,0.01,0.0,0.0,0.0};
			  vector<float> roots (myints, myints + sizeof(myints) / sizeof(float) );

			  		
				ofstream dataout(out);		    
			   
			  	  dataout << "0.0" <<'\t'<< "2.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.8 <<'\t'<< roots[0] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
			      dataout << "2.0" <<'\t'<< "5.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.2 <<'\t'<< roots[1] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
				  dataout << "5.0" <<'\t'<< "10.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[2] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
				  dataout << "10.0" <<'\t'<< "20.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[3] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
				  dataout << "20.0" <<'\t'<< "30.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[4] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
				  dataout << "30.0" <<'\t'<< "45.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[5] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH <<endl;
				  dataout << "45.0" <<'\t'<< "60.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[6] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH <<endl;
				  dataout << "60.0" <<'\t'<< "75.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[7] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
				  dataout << "75.0" <<'\t'<< "90.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[8] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
				  dataout << "90.0" <<'\t'<< "105.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[9] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
				  dataout << "105.0" <<'\t'<< "120.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[10] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
				  dataout << "120.0" <<'\t'<< "150.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[11] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
				  dataout << "150.0" <<'\t'<< "180.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[12] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;

				  

			  }


		cout<<"soil_ID_"<<Array[a]<<"Created"<<endl;
  

		}

		
	//}



		  system("pause");
}
Where is it failing? Does the file exist? Can the program find it? Is the failure after the file is found? .... There isn't much to go on.
Sorry!!

this is generating only one output file. soils_ID_1.txt with data of all outputfiles in it; however other output files are generated but no data in them!!
all right, I can't see your mistake there, but I can tell you something, the fstream and the iostream are very slow librarys , use fopen or freopen, are more fasters, or explain your problem better, sorry I can understand you...
I've reformatted your code and removed extra blank lines so I can see the code.

I've also highlighted the use of your input file. With the corrected indentation, the poissiblilty of the seeing most of the code on one page, and highlighting the use of your input, it ought to be clear why only one file is being created.

If it's still not clear, let use know and someone will point it out.
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
#include <fstream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <sstream>
#include <direct.h>
#include <string.h>
#include <math.h>

using namespace std;

vector <float> calc_PTF(int soil_indx);
int soil_type(float S,float U,float T);

void main()
{
	stringstream in;
	in<<"hwsd_soil_type_mainUK_"<<11<<".txt";
	string inputfilename = in.str();

	stringstream temp;
	temp<<"temporary_"<<11<<".txt";
	string temporary = temp.str();

	stringstream dd;
	dd<<"mainUK_soilfile_"<<11;
	string dir=dd.str();
	mkdir(dir.c_str());

	ifstream input(inputfilename);
	ifstream tempo(temporary);			

	int Array[38];

	for (int a=0; a<=37; a++)
	{
		tempo>>Array[a];
		stringstream outfilename;
		outfilename<<dir<<"/soils_ID_"<<Array[a]<<".in";
		string out = outfilename.str();

		int ID, east, nort, n_soil,share, s_sand, s_silt, s_clay, sft;
		float t_sand, t_silt,t_clay, t_oc,t_bulk, t_ph, s_bulk, s_oc, s_ph;

		while (input>>ID>>east>>nort>>n_soil>>share>>t_sand>>t_silt>>t_clay>>t_bulk>>t_oc>>t_ph>>s_sand>>s_silt>>s_clay>>s_bulk>>s_oc>>s_ph>>sft)
		{
			float S,U,T;
			float swc_r, swc_s, WP, FC, sat_cond;
			float omat, pH, bd;
			int soil_indx;
			vector <float> soil_data;

			S = t_sand;
			U = t_silt;
			T = t_clay;

			soil_indx = soil_type(S,U,T);
			soil_data = calc_PTF(soil_indx);

			S= S/100;
			T= T/100;
			U= U/100;
			pH = t_ph;
			bd = t_bulk;
			omat = (t_oc*1.72)/100;

			swc_r = soil_data[0];
			swc_s = soil_data[1];
			WP = soil_data[2];
			FC = soil_data[3];
			sat_cond = soil_data[4];
			float myints[] = {0.01,0.04,0.25,0.30,0.10,0.05,0.04,0.03,0.02,0.01,0.0,0.0,0.0};
			vector<float> roots (myints, myints + sizeof(myints) / sizeof(float) );

			ofstream dataout(out);
			dataout << "0.0" <<'\t'<< "2.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.8 <<'\t'<< roots[0] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
			dataout << "2.0" <<'\t'<< "5.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.2 <<'\t'<< roots[1] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
			dataout << "5.0" <<'\t'<< "10.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[2] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
			dataout << "10.0" <<'\t'<< "20.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[3] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
			dataout << "20.0" <<'\t'<< "30.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[4] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
			dataout << "30.0" <<'\t'<< "45.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[5] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH <<endl;
			dataout << "45.0" <<'\t'<< "60.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[6] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH <<endl;
			dataout << "60.0" <<'\t'<< "75.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[7] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
			dataout << "75.0" <<'\t'<< "90.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[8] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
			dataout << "90.0" <<'\t'<< "105.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[9] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
			dataout << "105.0" <<'\t'<< "120.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[10] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
			dataout << "120.0" <<'\t'<< "150.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[11] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
			dataout << "150.0" <<'\t'<< "180.0" <<'\t'<< bd <<'\t'<< FC <<'\t'<< WP <<'\t'<< 0.0 <<'\t'<< roots[12] <<'\t'<< S <<'\t'<< T <<'\t'<< omat <<'\t'<< swc_r <<'\t'<< sat_cond <<'\t'<< pH << endl;
		}
		cout<<"soil_ID_"<<Array[a]<<"Created"<<endl;
	}
	system("pause");
}

Last edited on
Thank you very much!! Ya!! I found the problem was in using the 'for loop' inside the 'while loop'.

thanks a lot
Topic archived. No new replies allowed.