C File Write: Save Results to Files

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 *file;
    int num = 10;

    file = fopen("output.txt", "w");
    if (file == NULL) {
        printf("Error opening file\n");
        return 1;
    }

    fprintf(file, "The result is: %d\n", num);

    fclose(file);

    return 0;
}

In this example, we start by opening a file using the fopen function and specifying the mode as “w” for write mode. Then, we use the fprintf function to write the results to the file. Finally, we close the file using the fclose function.

It is important to note that the writing mode will erase the contents of the file; if you want to append content to the end of the file, you can use the “a” mode.

Additionally, it is important to ensure that the file operation is successful, which can be done by checking the return value of the fopen function to determine if the file has been successfully opened.

bannerAds