07-25-2023, 01:24 PM
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");
// 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");