Create numbered files in C

I need to create files with the same name but with a number attached to the end of the file name to indicate that it was the nth file. So in the 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, this is not a way to do this, but any ideas on how I can accomplish this task?

Thank you
Christo

+3
source share
3 answers

How about something like that?

char filename[256];
int i = 1;

// codes omitted...
sprintf(filename, "file%4d", i);
+2
source

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

+3
source

You can use sprintf.

+1
source

Source: https://habr.com/ru/post/1732820/


All Articles