C Binary Files

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Binary Files?

fwrite(ptr, size, count, fp) writes count items of size bytes from memory; fread does the opposite. Both return the number of items transferred. Binary files are compact and fast but not human readable, and layouts (size, padding, endianness) may differ between systems.

Remember
FILE *fp = fopen("data.bin", "wb");
fwrite(arr, sizeof arr[0], n, fp);   /* returns items written */
fclose(fp);

fread(buf, sizeof buf[0], n, fp);    /* returns items read */

Cheat Sheet

Keep these facts handy while you work with Binary Files in C.

TopicDetail
Modes"rb", "wb", "ab"
ReturnItem count, compare to the request
PortabilityBeware padding and endianness
StructsWrite fields, or fixed-size, padded-free records

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Random File Access.

Example 1 — Write four integers and read them back

Check that fread returns the full count.

C
#include <stdio.h>

int main(void) {
    int out[] = {10, 20, 30, 40};
    FILE *fp = fopen("nums.bin", "wb");
    if (!fp) return 1;
    fwrite(out, sizeof out[0], 4, fp);
    fclose(fp);

    int in[4];
    fp = fopen("nums.bin", "rb");
    if (!fp) return 1;
    size_t got = fread(in, sizeof in[0], 4, fp);
    fclose(fp);

    printf("read %zu ints:", got);
    for (size_t i = 0; i < got; i++) printf(" %d", in[i]);
    printf("\n");
    return 0;
}

How It Works

  1. fwrite copies 16 bytes (4 ints) to the file.
  2. fread returns how many whole items it read.
  3. Reading with the same type and platform restores the values exactly.

Edge Cases & Pitfalls

Check these before relying on Binary Files in real programs.

text

Opening binary files in text mode

On Windows, text mode translates newlines and corrupts binary data. Always use "rb"/"wb".

pad

Writing structs directly

Padding bytes and endianness make raw struct files non-portable.

Key Takeaways

  • Items: size and count.
  • Mode: add b.
  • Portable: define a file format.

One line: fwrite(ptr, size, count, fp) writes count items of size bytes from memory; fread does the opposite.

Frequently Asked Questions

fwrite writes raw bytes exactly as stored in memory; fprintf converts values to readable text.
Yes, but the file is only readable by a program with the same struct layout, endianness, and padding. Write fields individually for portable formats.
Continue with "C Random File Access" next. Jump around a file with fseek, ftell, and rewind. The C File Handling hub lists every topic in order.

Next: Random File Access

Jump around a file with fseek, ftell, and rewind.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful