QP School
Reading and writing files in C - Printable Version

+- QP School (https://qomplainerzschool.lima-city.de)
+-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3)
+--- Forum: C 18 Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=32)
+--- Thread: Reading and writing files in C (/showthread.php?tid=5098)



Reading and writing files in C - Qomplainerz - 07-25-2023

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);
}