You are using a version without Ads of this website. Please, consider donating:

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.

 ``123456789101112131415161718`` `````` 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<
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.