This is probably trivial question. I am not a professional programmer, I am rather a mathematician who is doing some numerical experiment using C. I would like the output of my
You can just build a string up with sprintf
. Make sure your buffer is large enough:
char filename[20];
sprintf( filename, "file%d", i );
Then you can open it like this:
FILE *f = fopen( filename, "w");
...
fclose(f);
No need to use an array (if that's what you were trying to do with f(i)
), because you're only keeping one file open at a time.
If you want your files to be text-sortable, you might want file001
, file002
etc... You can use %03d
instead of %d
to 0-pad to 3 digits.