Possible Duplicate:
Easiest way to convert int to string in C++
How can I insert int variable while creating .vtk file? I want to create file at every k step. i.e. so there should be series of files, starting from file_no_1.vtk, file_no_2.vtk, ... to file_no_49.vtk .
while(k<50){
  ifstream myfile;
  myfile.open("file_no_.vtk");
  myfile.close();
  k++;
}
                In C++11:
while(k<50){
  ifstream myfile("file_no_" + std::to_string(k) + ".vtk");
  // myfile << "data to write\n";
  k++;
}
                        Use a stringstream (include <sstream>):
while(k < 50){
    std::ostringstream fileNameStream("file_no_");
    fileNameStream << k << ".vtk";
    std::string fileName = fileNameStream.str();
    myfile.open(fileName.c_str());
   // things
    myfile.close();
    k++;
}
                        Like this:
char fn [100];
snprintf (fn, sizeof fn, "file_no_%02d.vtk", k);
myfile.open(fn);
Or, if you don't want the leading zero (which your example shows):
snprintf (fn, sizeof fn, "file_no_%d.vtk", k);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With