Insert char to filename

I have lots of files needed for program named text1,text2, etc. I tried to do a loop like this:
1
2
3
4
5
ofstream of;
for(char i='1';i<'9';i++)
{
    of.open("text"<<i<<".txt");
}
I tried without << too, but it doesn't work neither. How can I make it work, without writing a line for each operation?
Last edited on
Use a std::string so you can + the strings together.
1
2
3
4
5
6
7
8
9
10
11
std::ofstream of;
std::string str = "text.txt";

for( char i = '1'; i <= '9'; ++i )
{
     std::string str_t = str;
     str_t.insert( 4, 1, i );

      of.open( str_t.c_str() );
}


Last edited on
Thanks! This worked!
Topic archived. No new replies allowed.