How to save the results to a file in C language?

In C language, you can use standard library functions to save the results to a file. Here is a simple example:

#include <stdio.h>

int main() {
    FILE *fp;
    int number = 10;

    // 打开文件,如果文件不存在则创建新文件
    fp = fopen("result.txt", "w");

    // 检查文件是否成功打开
    if (fp == NULL) {
        printf("无法打开文件\n");
        return 1;
    }

    // 将结果写入文件
    fprintf(fp, "结果: %d\n", number);

    // 关闭文件
    fclose(fp);

    printf("结果已保存到文件\n");

    return 0;
}

In the example above, the “fopen” function is first used to open a file named “result.txt”, specifying that it will be opened for writing. If the file does not exist, a new file will be created. Next, the “fprintf” function is used to write the results to the file. Finally, the “fclose” function is used to close the file.

After running this code, a file named “result.txt” will be generated in the current directory, and the results will be written into it.

bannerAds