File Input and Output

#include <stdio.h>

int main() {
    // File input and output
    FILE *file;
    char data[] = "Hello, File Handling!\n";

    // Writing to a file
    file = fopen("output.txt", "w");
    if (file != NULL) {
        fprintf(file, "%s", data);
        fclose(file);
        printf("Data written to the file.\n");
    } else {
        printf("Unable to open the file.\n");
    }

    // Reading from a file
    file = fopen("output.txt", "r");
    if (file != NULL) {
        char buffer[100];
        fgets(buffer, sizeof(buffer), file);
        fclose(file);
        printf("Data read from the file: %s", buffer);
    } else {
        printf("Unable to open the file.\n");
    }

    return 0;
}
chevron_left
chevron_right

Leave a comment

Your email address will not be published. Required fields are marked *

Comment
Name
Email
Website