There are about 50 files that need to be opened in my program for reading and i renamed all of them from 1.txt to 50.txt hoping i can pass the filename through a loop that increments the file number, but i don’t know how / don’t think it is possible to pass an integer to the char or is there a better way to workaround my situation.
char* filename = "";
for(int i =0; i < 50; i++)
{
if(i == 0){filename = "0.txt";}
if(i == 1){filename = "1.txt";} // ..
int num = 0, theinteger = 0;
ifstream in(filename, ios::binary);
unsigned char c;
while( in.read((char *)&c, 1) )
{
in >> theinteger;
sca.chac[num]=theinteger;
num++;
}
}
return 0;
There is a relatively straightforward way to do it – in C, use
sprintffunction, like this:In C++, use
ostringstream: