Help needed in writing files

Hi,

I have posted part of my code below.

Here, I need to write a five different text files. I have managed to create five different files.

But the problem is that it should write 40 rows of values. But, I'm getting only 1 row of values in all five files created.

Can anyone share me how to write all of my values in each file.

Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 for (int t=2;t<=1.082/dt;t++)
{
      std::copy(y,y+n,uy);
      for(int i=2;i<=j-1;i++) //j=41
      {
      y[i]=uy[i]+((mu*dt/(2*dy*dy))*(uy[i+1]-(2*uy[i])+uy[i-1]));
      if ((t==91)||(t==181)||(t==271)||(t==361)||(t==451)||(t==541))
	{
	 ofstream myfile;
         stringstream ss;
	 ss << "Result-" << t << ".txt";
	 myfile.open (ss.str().c_str());
	 myfile<<std::setprecision(16)<<y[i]<<'\t';
         myfile<<(i-1)*dy<<endl;
	 myfile.close();
	}
      }
}
Last edited on
Your for loop at line 1 is suspect.

You don't show what type dt is, but since t is defined as an int, the calculation 1.082/dt and comparison to t will be truncated to an int. It's not clear why you're not using a simple for loop since you want to write files 1 through 5.
 
for (int t=1; t<=5; t++)


dt=0.002

Actually I want to calculate y[i] which is varying for every iteration of t for different point values i.

so, i cant run the code for only i=1 to 5. which will fail my case.

Calculation part is not problem. problem is in if loop. please help me to write file if only it passes if loop in the nested loop structure. that too only 5 files i dont want to write n number of files.

My assumption about files 1-5 was based on your other thread asking about naming the files.

In this case, the if statement at line 7, will result in writing once to each of six files.
The files will be result-91, result-181, result-271, result-361, result-451 and result-541. i.e. the if statement checks t for one of six possible values, and only if t matches does it open a file with that suffix, does one write to it and closes it.

I'm guessing that you're counting on the for loop at line 4 to iterate through the 40 values, although that is far from clear. Since it's not clear how you want to separate the values into 5 files, I can't suggest anything at this point.

Topic archived. No new replies allowed.