QP School

Full Version: Reading and writing files in C
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
C provides functions for reading from and writing to files.

// Writing to a file
FILE *file = fopen("data.txt", "w");
if (file != NULL) {
    fprintf(file, "Hello, World!");
    fclose(file);
}

// Reading from a file
char buffer[100];
file = fopen("data.txt", "r");
if (file != NULL) {
    fgets(buffer, 100, file);
    printf("%s\n", buffer); // Output: Hello, World!
    fclose(file);
}