tags:

views:

82

answers:

3

I need to create files that have the same name, but with a number attached to the end of the filename to indicate that it was the nth file made. So in a for loop, I basically want to do this:

char *filename = "file";
strcat(filename, i); // put the number i at the end of the filename

Clearly that isn't the way to do it, but any ideas as to how I can accomplish this task?

Thanks,
Hristo

+3  A: 

sprintf() or snprintf() with "file%d".

Ignacio Vazquez-Abrams
I would've been first if it weren't for SO's short answer limit (and the captcha if you edit too fast). Grrr! :-)
Laurence Gonsalves
+1  A: 

You can use sprintf.

Laurence Gonsalves
+2  A: 

How about something like this?

char filename[256];
int i = 1;

// codes omitted...
sprintf(filename, "file%4d", i);
shinkou
Better use `%04d` unless you like whitespaces in file names
mouviciel
@mouviciel yes, that was what I wanted to type, my fingers didn't listen to me. :P
shinkou
thanks a bunch!
Hristo