code for generating multiple files with different values from one file

My input file has the following information
10 20 30
100 200 300
1000 2000 3000

The first row represents the word city. ie. city can have possible values of 10, 20 and 30. The second row represents temperature, which can have possible values of 100, 200 and 300. The third row represents recommended which has possible values of 1000, 2000 and 3000. I want to create multiple files with each file containing one combination of the 3 values. ie.

city 10
temperature 100
recommended? 1000
is one file
city 10
temperature 100
recommended? 2000
is another file and so on. So there will be a total of 27 files, containing all possible combinations of city, temperature and recommended. When I run the code I am able to get 27 files with just one combination.

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
  //Just writing the program to create the svrf file to and the last 9001 to the other layers
#include <iostream>
#include <fstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <map>
#include <string>
#include <cassert>
#include <algorithm>
#include <iterator>
#include <sstream>
using namespace std;

const int MAX_SIZE_X = 20;
const int MAX_SIZE_Y = 20;
int ncol=3;

int INPUTLAYER[MAX_SIZE_X][MAX_SIZE_Y];
//int CALIBRELAYER[MAX_SIZE];
int i;
int j;
int l, m, n;
int nrow=4;

int maximumIJvalue = 27;


int main()
{
	// Create variables to store matrix information in 
	int num, a, b, filename;
	ifstream fin;		// Create input and output file streams ifstream fin; ofstream fout
	fin.open("ocean_dummy");
    if(fin)
    {
        //string INPUTLAYER[nrow][2];

        for(int i = 0; i < nrow; ++i)
        {
	for (int j = 0; j <ncol; ++j)
	{
            fin >> INPUTLAYER[i][j];
	}
        }
    }
	

      	ofstream fout;

for(filename = 1; filename <= maximumIJvalue; filename++) {
	stringstream inputname;
	inputname << filename;
	string str = inputname.str();
	
	fout.open(str.c_str());
	
     	
		for (l=0; l < ncol; l++)
		{
			str += ".txt";	
			
			for (m=0; m < ncol; m++) {
				for (n=0; n < ncol; n++) {

								
								
								
      								
      
  										fout<<"City "<< INPUTLAYER[0][l] << "\n";
										fout<<"Temperature "<< INPUTLAYER[1][m] << "\n";
										fout<<"recommended? "<< INPUTLAYER[2][n] << "\n";

									
      								fout.close();
								}
				
				}
			
		       }
		
		}
	fin.close();
	

	return 0;
}
At line 76 you have fout.close();
The file open should also be in that same block of code, within that innermost loop, immediately before the fout << statements.

Something 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
    ofstream fout;

    int filename = 0;     
    
    for (l=0; l < ncol; l++)
    {
        for (m=0; m < ncol; m++) {
            for (n=0; n < ncol; n++) {
                
                filename++;
                
                stringstream inputname;
                inputname << filename << ".txt"; 
                string str = inputname.str();
                
                fout.open(str.c_str());                    
                
                fout<<"City "<< INPUTLAYER[0][l] << "\n";
                fout<<"Temperature "<< INPUTLAYER[1][m] << "\n";
                fout<<"recommended? "<< INPUTLAYER[2][n] << "\n";
                
                fout.close();
            }
        }       
   }


Last edited on
Or this, (uses some c++11 syntax)
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

void readfile(vector<int> &vec, ifstream & file)
{
    string line;
    getline(file,line);
    istringstream ss(line);
    int n;
    while (ss >> n)
        vec.push_back(n);
}

string filename()
{
    static int n = 0;
    string base = "ocean_out_";
    ostringstream sout;
    sout << setfill('0') << base << setw(3) << n++ << ".txt";
    return sout.str();
}

int main()
{
    ifstream fin("ocean_dummy.txt");
    
    vector<int> city;
    vector<int> temperature;
    vector<int> recommended;
    
    readfile(city, fin);
    readfile(temperature, fin);
    readfile(recommended, fin);
    
    for (int c : city)
      for (int t : temperature)
        for (int r : recommended)
        {
            ofstream fout(filename());
            fout << "City "         << c << '\n'
                 << "Temperature "  << t << '\n'
                 << "recommended? " << r << '\n';
        } 
        
}
Last edited on
Topic archived. No new replies allowed.