multiple out files, is there a way to shorten 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
	outFile1.open(patho1, ios::app);		//creates new blank file for export
	if (outFile1.fail())
		{
			cout<< "Output file failed to open";
			exit(2);				//leaves program # is the error code
		} 

	outFile1.setf(ios::fixed);
	outFile1.setf(ios::showpoint);
	outFile1.precision(2);

	outFile2.open(patho2, ios::app);		//creates new blank file for export
	if (outFile2.fail())
		{
			cout<< "Output file failed to open";
			exit(2);				//leaves program # is the error code
		} 

	outFile2.setf(ios::fixed);
	outFile2.setf(ios::showpoint);
	outFile2.precision(2);

	outFile3.open(patho3, ios::app);		//creates new blank file for export
	if (outFile3.fail())
		{
			cout<< "Output file failed to open";
			exit(2);				//leaves program # is the error code
		} 

	outFile3.setf(ios::fixed);
	outFile3.setf(ios::showpoint);
	outFile3.precision(2);

	outFile4.open(patho4, ios::app);		//creates new blank file for export
	if (outFile4.fail())
		{
			cout<< "Output file failed to open";
			exit(2);				//leaves program # is the error code
		} 

	outFile4.setf(ios::fixed);
	outFile4.setf(ios::showpoint);
	outFile4.precision(2);

	while(!inFile.eof())
	{
		inFile >> name_first >> name_last >> dep >> wage;
				switch (dep)
				{
					case 1: 
						outFile1 << name_last << ", " << name_first << " "<< wage<<endl;
						break;
					case 2:
						outFile2 << name_last << ", " << name_first << " "<< wage<<endl;
						break;
					case 3:
						outFile3 << name_last << ", " << name_first << " "<< wage<<endl;
						break;
					case 4:
						outFile4 << name_last << ", " << name_first << " "<< wage<<endl;

					default:
				break;
		}
	}

	inFile.close();
	outFile1.close();
	outFile2.close();
	outFile3.close();
	outFile4.close();


I am trying to think of a way to shorten this. I am thinking a function, or pointers but I am not exactly sure how. Thanks :D
Why don't you put all of the data to be written into a std::stringstream ( http://cplusplus.com/reference/iostream/stringstream/ ) and then have a function that takes a filename to write to and the data to put into it.
That would be because I have not gotten to std::stringstream yet.

I am going with what I know, and practicing this until I have a good handle on it.

Also, it isn't using iostream for the most part. This program reads a file, and outputs to four other files depending on the data. The data also isn't just a string but also numbers.

Last edited on
So playing with this I came up this an Idea to use arrays such as outFile[4], and patho[4] to create, sort, and close my open files. In theory it would cut down on the amount of code I have up above. Then just use loops to rotate through my four sets.

1
2
3
4
5
6
7
8
9
10
11
12
 
string patho(i)
{
   string temp="patho", a;
    a = i;
    strncpy(temp, a)
    return temp;
};
 
ofstream outFile[i];
for (int i = 0; i < 4; i++)
    outFile[i].open(patho(i));


I'm not a 100% sure on this but I think this may work.
Last edited on
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
	
         for (i=0; i<4; i++)
	{	
		ofstream outFile[i];
	}


	for (int i = 0; i < 4; i++)
	{
		outFile[i].open(patho(i).c_str(), ios::app);
			if (outFile[i].fail())
			{
			cout<< "Output file failed to open";
			exit(2);				//leaves program # is the error code
			} 
	}

	inFile.open(pathi);		//start file, and location relative to program
	if(inFile.fail())
		{
			cout<<"Input file failed to open.\n";
			exit(1);
		}

	while(!inFile.eof())
	{
		inFile >> name_first >> name_last >> dep >> wage;
				switch (dep)
				{
					case 1: 
						outFile[0] << name_last << ", " << name_first << " "<< wage<<endl;
						break;
					case 2:
						outFile[1] << name_last << ", " << name_first << " "<< wage<<endl;
						break;
					case 3:
						outFile[2] << name_last << ", " << name_first << " "<< wage<<endl;
						break;
					case 4:
						outFile[3] << name_last << ", " << name_first << " "<< wage<<endl;

					default:
				break;
		}
	}

	inFile.close();

	for (i=0; i<4; i++)
	{	
		outFile[i].close();
	}

	cout<< "End of editing files. \n";
}

string patho(int i)
{
	string temp="patho", a;
	a = i;
	strncpy(temp, a);
	return temp;
}


So there are a couple problems, which is mostly that my outFile[i] array isn't a constant. IE a name.

So I think I want a function that returns a converted string as the name. Am I on the right track?
Topic archived. No new replies allowed.