Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text and binary files in C
#1
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");
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating and using header files in C Qomplainerz 0 267 07-25-2023, 01:29 PM
Last Post: Qomplainerz
  Reading and writing files in C Qomplainerz 0 239 07-25-2023, 01:23 PM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 6 Guest(s)