How to use index in the file name

前端 未结 4 1539
迷失自我
迷失自我 2021-01-24 09:12

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

4条回答
  •  清酒与你
    2021-01-24 09:37

    In C, use snprintf:

    char buf[PATH_MAX];
    snprintf(buf, sizeof(buf), "file%d", i);
    

    If you use linux, there is a useful GNU extension:

    char *name;
    asprintf(&name. "file%d", i);
    

    You need to remember to free(name) after use.

    Note that your syntax FILE *f(i); is not valid though.

    If you need to declare an array of FILE * of 10 elements do:

    FILE *array[10];
    

    then use it like that:

    array[i] = fopen(filename, "W");
    

提交回复
热议问题