Unit 5 · Lesson 218 minAcademic review pending

Storing records exactly as they are in memory

A binary file stores raw bytes rather than readable text, which makes whole structures easy to save and makes every record the same size.

Choose explanation

After this lesson

You should be able to

  • Open a file in binary mode and write a structure with fwrite.
  • Read records back with fread and detect the end of the file.
  • Explain the trade-off between text and binary files.
01

Bytes, not characters

A text file stores the number 87 as the two characters '8' and '7'. A binary file stores the four bytes that make up the int itself. That means no conversion on the way in or out, and the same number of bytes for every value.

The cost is readability. Open a binary file in a text editor and you see nonsense — you cannot inspect or fix it by eye, which is a real disadvantage while learning.

fwrite takes an address, a size and a count
FILE *fp = fopen("students.dat", "wb");   /* b = binary */

Student s = {101, "Kavya", 87};

fwrite(&s, sizeof(Student), 1, fp);       /* address, size, count, file */

fclose(fp);
02

Reading records back

fread mirrors fwrite and returns how many items it actually read. Looping while that equals 1 reads every record and stops cleanly at the end — the same pattern as fscanf, for the same reason.

Because every record is exactly sizeof(Student) bytes, record number i starts at i times that size. That fixed size is what makes direct access possible, and it is the subject of the next lesson.

One record per pass, until fread runs out
FILE *fp = fopen("students.dat", "rb");
Student s;

while (fread(&s, sizeof(Student), 1, fp) == 1)
    printf("%d %s %d\n", s.roll, s.name, s.marks);

fclose(fp);
03

Which to choose

Use text when a human or another program must read the file, or when you want to inspect it while debugging. Use binary for many fixed-size records that only your program will touch, especially when you need to jump to record number 500 without reading the first 499.

Binary files are not portable between machines with different int sizes or byte orders. For a course exercise that does not matter; for data you intend to keep, it does.

Try it yourself

Write three student records to a binary file, then read them back and print them.

Need a hint?

You can write the whole array in one fwrite by passing a count of 3.

Check the worked solution

Writing all three in one call and reading them back one at a time both work, because fwrite and fread only deal in counts of a fixed record size. The read loop uses fread's return value, so it stops exactly at the end of the file regardless of how many records were written.

#include <stdio.h>

typedef struct {
    int  roll;
    char name[40];
    int  marks;
} Student;

int main(void)
{
    Student class[3] = {
        {101, "Kavya", 87}, {102, "Ravi", 64}, {103, "Sneha", 91}
    };
    Student s;
    FILE *fp;

    fp = fopen("students.dat", "wb");
    if (fp == NULL) { printf("Cannot write\n"); return 1; }
    fwrite(class, sizeof(Student), 3, fp);
    fclose(fp);

    fp = fopen("students.dat", "rb");
    if (fp == NULL) { printf("Cannot read\n"); return 1; }
    while (fread(&s, sizeof(Student), 1, fp) == 1)
        printf("%d %-8s %d\n", s.roll, s.name, s.marks);
    fclose(fp);

    return 0;
}

Quick check

How does a binary file store the integer 87 differently from a text file?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

Binary Files

Maps to course outcomes CO3, CO5.