output multiple files by loops. Please help


I have got some help with this topic. but still can't solve it.

I need to output 4 data files for each different value of ka={0, 1, 2, 3};
each output file include 7000 pairs of x,y value.
outputs names shall be like 'output_ka0.out', 'output_ka1.out', 'output_ka2.out', 'output_ka3.out'

I am using for loop. here is my code with someone's help. I don't know how to handle strings. error is pointing to 'ofstream' in function fileoutsetup ().
Please help. thanks so much

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
#include <iostream.h>
#include <math.h>
#include <fstream.h>
#include <stdlib.h> 
#include <cmath>
#include <string>
#include <sstream>
using namespace std;

void inputs(void);
void fileoutsetup(void);
void fileout (void);
double computation ();

int prmtIND, t;
double x[7000], y[7000], ka, parameter[] = {0.0, 1.0, 2.0, 3.0};	

main ()
{
	for (prmtIND=1; prmtIND<5 ;prmtIND++)		
	{
		inputs();
		fileoutsetsup();
		computation(); 
	}
	return 0;
}

void inputs (void)
{	
	ka = parameter [prmtIND];
}

void fileoutsetsup(void)
{	
         stringstream ss;
	 ss <<"output_ka="<< ka << ".out";					
         ofstream myout(ss.str(),ios::trunc|ios::app);	
}

double computation()
{
	for (t=1;t<=7000;t++)
	{	y[t] = x[t]*ka;
		fileout();
	}
}


void fileout(void) 
{
	stringstream ss;	
	ss<<"output_ka="<<ka<<".out";					
	ofstream myout(ss.str().c_str(),ios::app);				
	
	myout<< x[t] << '\t' << y[t] << '\n';
}

That is some horrid code with all those globals.

You never set the values in x to anything, and since they're initialized to 0 and the values you put in y are the values in x * some_number, there will never be anything but 0s in either array.

Also, line 43 should be: for ( t=0; t<7000; t++) You are currently accessing memory you shouldn't be.

error is pointing to 'ofstream' in function fileoutsetup ()


ofstream myout(ss.str().c_str(), ios::trunc | ios::app) ;
Last edited on
sorry I didn't paste x calculation actually. that was way too much code to read.
I actually kind of give example what I am doing inside computation()

thanks for the missing part.
I will try it


it now outputs number of files I want. --- good news.
bad news is that output data is adding into the old data file every time I run it.

how can I solve this problem ?
I'm not sure, but I think the problem may be line 38. ios::trunc and ios::app don't make much sense together. ios::app implies there is data in the file you need to seek past.

Of course, your fileout functions don't make much sense anyway. =P

If it's possible to wait and do all the output after the computations are done, that is probably preferable. file output that includes opening and closing a file in the middle of your main loop isn't really very smart.

Something like:

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
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib> 
#include <cmath>
#include <string>
#include <sstream>
using namespace std;

void inputs(void);
double computation ();
void writeOutputFile() ;

int prmtIND, t;
double x[7000], y[7000], ka, parameter[] = {0.0, 1.0, 2.0, 3.0};	

main ()
{
	for (prmtIND=1; prmtIND<5 ;prmtIND++)		
	{
		inputs();
		computation();
		writeOutputFile() ;
	}
	return 0;
}

void inputs (void)
{	
	ka = parameter [prmtIND];
}

double computation()
{
	for (t=1;t<=7000;t++)
	{	
		y[t] = x[t]*ka;
                // no file output here
	}
}


void writeOutputFile()
{
	ostringstream s ;
	s << "output_ka=" << ka << ".out" ;

	ofstream out(s.str().c_str(), ios::trunc|ios::out) ;

	for ( unsigned i = 0 ; i < 7000 ; ++i )
		out << x[i] << '\t' << y[i] << '\n' ;
}







thanks a lot, it works.

I simply need to change 'app' to 'out' in my original 'fileoutsetsup ()' function

output still can be inserted into loops of computation.

Topic archived. No new replies allowed.