How can I open files with names like ...

Q

How can I open files with names like ``file1'', ``file2'', ``file3'', etc., where the numeric part is controlled by a variable? Basically I want ``file%d'', like printf..

✍: Guest

A

You want printf's close cousin sprintf, which ``prints'' to a string:
char filename[FILENAME_MAX];
sprintf(filename, "file%d", i);
fp = fopen(filename, "r");

2015-10-07, 1069👍, 0💬