how to creat a set of data output file ?


Please help me, dear experts

I am using for loop to calculate a set of output with different parameter value in 'a'.

a = {1.2, 2.2, 3.2, 4.2};
for diff value of a, I need to output a file. totally 4 output files as following:
" output_a1.out", " output_a2.out", " output_a3.out" and " output_a4.out"

and each file include 5000 pairs of x, y data.
many many thanks to you guys.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
main()
    {	
        a = 1.2;
	for (N=1;N<5000;N++)
	{
	 Y[N] = X[N]+sin(a); /* X has been defined somewhere else */
	 fout3(N);
	}
    }

void fout3(int N)
 {       ofstream ff("output_a1.out",ios::app);
	 ff <<  x[N] <<  '\t' << y[N] << '\t' << '\n';
  }

**********************************************************************
Last edited on
1
2
3
4
5
6
7
void fout3(int N)
{
  stringstream ss;
  ss << "output_a" << N + 1 << ".out";
  ofstream ff( ss.str().c_str(), ios::app );
  ff << x[N] << '\t' << y[N] << '\t' << '\n';
}

thanks, the string process is what I need !

but this 'fout3()' will be used in 5000 loops. turns out will be 5000 output files. I only need 4 output files for each different 'a' value.

did I miss something?
based on binarybob350's help, I modify something so that I can have only 4 output files for each different 'a' value and each file include 5000 pairs of x,y data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

main()
{
     a = {1.2, 2.2, 3.2, 4.2};
     for (i=1;i<=4;i++)
      {
             for (N=1;N<5000;N++)
              {
                     Y[N] = X[N]+sin(a[i]); /* X has been defined somewhere else */
                      fout3(N,i);
               }
       } 
}

void fout3(int N, int i)
{
  stringstream ss;
  ss << "output_a" << i << ".out";
  ofstream ff( ss.str().c_str(), ios::app );
  ff << x[N] << '\t' << y[N] << '\t' << '\n';
}
Last edited on
Topic archived. No new replies allowed.