Writing many txt files but with different numbers
Hi,
I'm trying to create five txt files from one program.
Exactly what I want to do is to create file name as follows
Result-1.txt
Result-2.txt
Result-3.txt
.............
Result-n.txt
See my code and correct me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
ofstream myfile;
myfile.open ("Result.txt");
for (int t=2;t<=1.082/dt;t++)
{
std::copy(y,y+n,uy);
for(int i=2;i<=j-1;i++)
{
y[i]=uy[i]+((mu*dt/(2*dy*dy))*(uy[i+1]-(2*uy[i])+uy[i-1]));
if ((t==90)||(t==180)||(t==270)||(t==360)||(t==450)||(t==540))
{
myfile<<std::setprecision(16)<<y[i]<<'\t';
myfile<<(i-1)*dy<<endl;
cout<<i<<'\t'<<t<<'\t'<<y[i]<<endl;
}
}
}
|
Last edited on
ofstream::open accepts a character array, or in C++11 a std::string.
Simply build a character array or string with the file name you want.
C-style:
1 2 3
|
char fname[20];
sprintf (fname, "Result-%d.txt", n);
myfile.open (fname);
|
C++ style:
1 2 3 4
|
stringstream ss;
ss << "Result-" << n << ".txt";
myfile.open (ss.str().c_str()); // C++
myfile.open (ss.str()); // C++11
|
Last edited on
Topic archived. No new replies allowed.