QP School
Text and binary 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: Text and binary files in C (/showthread.php?tid=5099)



Text and binary files in C - Qomplainerz - 07-25-2023

C supports both text and binary file handling.

// Writing to a binary file
int data[] = {10, 20, 30, 40, 50};
FILE *binary_file = fopen("data.bin", "wb");
if (binary_file != NULL) {
    fwrite(data, sizeof(int), 5, binary_file);
    fclose(binary_file);
}

// Reading from a binary file
int read_data[5];
binary_file = fopen("data.bin", "rb");
if (binary_file != NULL) {
    fread(read_data, sizeof(int), 5, binary_file);
    fclose(binary_file);
    for (int i = 0; i < 5; i++) {
        printf("%d ", read_data[i]); // Output: 10 20 30 40 50
    }
}
printf("\n");