0Pricing
C Academy · Lesson

Working with Binary Files

Understand how to handle non-text data using fread and fwrite.

Working with Binary Files is a free C Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Working with Binary Files

Binary files store data in raw format, which allows efficient reading and writing without converting data to text.

In this lesson, you will learn:

  • How to write data to a binary file using fwrite().
  • How to read data from a binary file using fread().
  • The difference between binary and text file handling.
Working with Binary Files — illustration 1

2

Opening a Binary File

Binary files are opened using the fopen() function with a mode containing b.

Example:

FILE *filePtr = fopen("data.bin", "wb");

This opens data.bin for writing in binary mode.

3

Writing to a Binary File

The fwrite() function writes binary data to a file.

Syntax:

fwrite(&data, size, count, filePtr);

Example:

fwrite(&num, sizeof(int), 1, filePtr);

This writes an integer to the file.

4

Example: Writing to a Binary File

This program demonstrates writing an integer to a binary file.

#include <stdio.h>

int main() {
    FILE *filePtr = fopen("data.bin", "wb");
    if (filePtr == NULL) {
        printf("Error opening file!\n");
        return 1;
    }
    int num = 42;
    fwrite(&num, sizeof(int), 1, filePtr);
    fclose(filePtr);
    return 0;
}

5

Reading from a Binary File

The fread() function reads binary data from a file.

Syntax:

fread(&data, size, count, filePtr);

Example:

fread(&num, sizeof(int), 1, filePtr);

This reads an integer from the file.

6

Example: Reading from a Binary File

This program demonstrates reading an integer from a binary file.

#include <stdio.h>

int main() {
    FILE *filePtr = fopen("data.bin", "rb");
    if (filePtr == NULL) {
        printf("Error opening file!\n");
        return 1;
    }
    int num;
    fread(&num, sizeof(int), 1, filePtr);
    printf("Number read from file: %d\n", num);
    fclose(filePtr);
    return 0;
}

7

Appending to a Binary File

Use ab mode to append binary data to a file:

FILE *filePtr = fopen("data.bin", "ab");

This keeps existing data and adds new data at the end of the file.

8

9

Text vs Binary Files

Text files:

  • Store data as human-readable characters.
  • Use fprintf() and fscanf().

Binary files:

  • Store data in raw format.
  • Use fwrite() and fread().

10

Summary

In this lesson, you learned:

  • How to write and read binary files using fwrite() and fread().
  • The difference between text and binary file operations.
  • How to append binary data to an existing file.

Next, we will explore error handling in file operations!

Working with Binary Files — illustration 10

Frequently asked questions

Is the “Working with Binary Files” lesson free?

Yes — the full text of “Working with Binary Files” is free to read here on the web, and the C Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Working with Binary Files”?

Understand how to handle non-text data using fread and fwrite. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Working with Binary Files” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to File Handling
  2. Working with Binary Files
  3. File Error Handling
← Back to C Academy