C Notes
Complete guide to binary file handling in C — reading and writing binary data with fread/fwrite, struct serialization, random access with fseek, database-like operations, practical examples.
Text files store everything as readable characters — the number 12345 takes 5 bytes (one per digit). Binary files store data in the same format as memory — the same number takes just 4 bytes (sizeof(int)). For structured data like records, images, or databases, binary files are faster, more compact, and easier to work with.
Why Binary Files?
| Aspect | Text File | Binary File |
|---|---|---|
Storage of int 12345 | 5 bytes ("12345") | 4 bytes (raw bits) |
Storage of float 3.14 | 4 bytes ("3.14") | 4 bytes (IEEE 754) |
| Reading speed | Parse strings → numbers | Direct memory copy |
| Struct storage | Must serialize each field | Write entire struct at once |
| Human readable | Yes | No |
| Portability | High | Low (endianness, padding) |
Writing Structures to Binary Files
Wrote 5 employee records (220 bytes total)
Reading Structures from Binary Files
ID Name Salary Dept ────────────────────────────────────────────── 1 Alice Johnson 85000.00 101 2 Bob Smith 72000.00 102 3 Charlie Brown 91000.00 101 4 Diana Prince 68000.00 103 5 Eve Wilson 79000.00 102
Random Access with fseek()
Binary files enable jumping directly to any record:
Record #3: id=3, name=Third, value=300.0
Memory Diagram: Binary File Layout
| Record 1 | Record 2 | Record 3 | Record 4 | Record 5 |
|---|---|---|---|---|
| 28 bytes | 28 bytes | 28 bytes | 28 bytes | 28 bytes |
Updating a Specific Record
Record updated!
Counting Records in a Binary File
Deleting a Record (Copy-Overwrite Method)
fseek() Modes
| Mode | Constant | Seeks from |
|---|---|---|
| Beginning | SEEK_SET | Start of file (offset 0) |
| Current | SEEK_CUR | Current position |
| End | SEEK_END | End of file |
fseek(fp, 0, SEEK_SET); // Go to beginning
fseek(fp, 0, SEEK_END); // Go to end
fseek(fp, 100, SEEK_SET); // Go to byte 100 from start
fseek(fp, -28, SEEK_CUR); // Go back 28 bytes from currentInterview Questions
Q1: Why can't you just use fprintf/fscanf for structs?
You could, but you'd have to serialize each field individually, handle formatting, and parse it back. Binary fwrite/fread copies the struct directly — one call writes the entire record exactly as it exists in memory. It's faster and simpler for fixed-size structures.
Q2: What are the portability issues with binary files?
Binary files depend on: (1) endianness (byte order — x86 is little-endian, some ARM is big-endian), (2) struct padding (varies by compiler and settings), (3) type sizes (int may be 2 or 4 bytes). A binary file written on one system may not read correctly on another.
Q3: How do you implement deletion in a binary file?
Two approaches: (1) Copy all records except the target to a new file, then replace the original (safe but slow). (2) Mark records as deleted with a flag field and skip them during reads (fast but wastes space — needs periodic compaction).
Q4: What is random access and why do binary files support it?
Random access means jumping directly to any position in the file without reading everything before it. Binary files support this because records have fixed sizes — record N is at offset N * sizeof(record). Text files have variable-length lines, making random access impractical.
Summary
- Binary files store data in raw memory format — compact and fast
fwrite()writes structs/arrays directly;fread()reads them backfseek()enables random access to any record by calculating offsets- Record N is at position
(N-1) * sizeof(struct)from the start - Use
"rb"and"wb"modes for binary operations - Binary files are not portable across different architectures
- For deletion, use copy-to-temp or mark-as-deleted approaches
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Binary Files in C – fread and fwrite.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, file, handling, binary, files, binary files in c – fread and fwrite
Related C Programming Topics