you are very close to getting ti working the problem is how you are converting an int
to char
assuming that you are only going from 0 to 9 you can have
//this is either the relative or absolute file path
std::string base_file = "data";
std::string after_number = ".txt"
for(int fid(0); fid < 10; ++fid)
{
char digit = '0' + fid;
std::string cur_filename = base_file + digit + after_number;
std::ifstream fin(cur_filename.c_str());
if(fin.is_open() == true)
{
//etc..
fin.close();
}
else
{
std::cout << "could not open file:" << cur_filename << std::endl;
}
}
the digit code will need expanding for larger number of files but it is just a file manipulation check what the string looks like before opening.
For example if I have this int type variable X that increment from 1 at the beginning to 10 when the program ends, how to open and write to 10 different files (data1.txt,data2.txt,data3.txt....) everytime X increments?
I've tried with the following but it didn't work:
string filename ("data"); int X; ofstream out; char number; for(X=1;X<11;X++) { number=X; filename+=number; out.open(filename.c_str()); .... ...writing.... out.close(); }