Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. c-programming
  3. serial-intro
5 min read

Serialized File I/O

This part of the programming section covers serialized data. Serialized data is stored in binary, meaning it is not readily human-readable. Since many of the bytes stored will not fall in the printable ASCII range, opening the file in a text editor does nothing for us — we have to read it as raw bytes and interpret it ourselves.

Each challenge will instruct us on how the data is formatted. We will typically be given a layout for the records that make up the file. Each field in a record has a specific size — either fixed or variable — and we use that layout to pull the bytes back out in the right order.

Once we have read the structure, we process it to recover the flag. This step is different for each challenge, but we will walk through each one. It is typically not the hard part, especially if you are comfortable with the ASCII half of the programming section.

Reading Data

When we read binary data, we pass the binary flag to fopen so the platform does not try to translate newlines or stop early on a stray byte:

c
1FILE *fp = fopen("data.bin", "rb"); // "rb" = read, binary
2if (fp == NULL)
3

To pull the bytes out we use fread, which lets us say exactly how many bytes we want and where to put them. It takes a destination pointer, the size of one element, how many elements to read, and the file pointer. It returns the number of elements actually read, which we check to know when we have hit the end of the file:

c
1uint8_t tag;
2while (fread(&tag, sizeof(tag), 1, fp) == 1)

Because the layout is fixed, we can often read a whole record straight into a struct:

c
1typedef struct
2{
3    uint16_t index;
4    uint8_t letter;
5    uint16_t left
Watch out for struct padding
The compiler is allowed to insert padding bytes between fields so they land on aligned addresses. A struct with a uint16_t, a uint8_t, then two more uint16_ts may be 8 bytes in memory but only 7 bytes on disk. When the on-disk size and the in-memory size differ, read each field individually instead of reading the whole struct at once — or pack the struct. Never assume sizeof(struct) == bytes on disk.

Endianness

A multi-byte integer can be stored with its least-significant byte first (little-endian, what x86 uses) or its most-significant byte first (big-endian, the traditional "network byte order"). The two orderings store the same number as different byte sequences, so reading with the wrong one gives you garbage.

The challenge instructions always tell you which ordering the file uses. If it matches the machine you are running on, fread into an integer just works. If it does not, read the raw bytes and assemble the value yourself so your code is correct regardless of the host:

c
1uint8_t b[2];
2fread(b, 1, 2, fp);
3uint16_t

Assembling values by hand like this is the safest habit: it never depends on the host's own byte order or on struct padding.

Fixed vs. Variable-Length Records

Serialized files come in two broad shapes, and almost every challenge is one of them:

  • Fixed-length records. Every record is the same number of bytes, so you can read them in a simple loop until fread runs out. A node that is always { index, letter, left, right } is the classic case — read one record, repeat.
  • Variable-length records. A record carries its own size. The most common form is TLV — Tag, Length, Value — where a one-byte tag says what the record is, a length says how many value bytes follow, and the value is exactly that many bytes. You read the tag and length first, then read length more bytes:
c
1uint8_t tag, length;
2while (fread(&tag, 1, 1, fp) == 1 

An opcode stream is a close cousin: a one-byte opcode decides how many bytes follow it, so you branch on the opcode and read the operands it implies.

Storing What You Read

As you read records, you need somewhere to keep them. We use two primary approaches, each with trade-offs:

  • Static Array. The easiest approach and the least overhead. It is the hardest to mess up and the easiest to clean up when the program ends. The downside is deciding how much space to allocate up front, which tends toward a non-space-efficient solution. This works for nearly every challenge.
  • Linked Lists. Best for challenges built around stacks, queues, and linked lists. It is space-efficient because you only use the memory you need, but it is more painful to build and worse to clean up. Be careful not to leak memory.

Nearly all of the challenges follow the same shape, so once you have a template for reading records and storing them, you can reuse it across the section. The same is largely true of the ASCII section.

Let's get solving!

{
4 printf("Could not open file.\n");
5 exit(EXIT_FAILURE);
6}
3{
4 // one byte was read into `tag` — interpret it
5}
;
6 uint16_t right;
7} Node;
8
9Node n;
10fread(&n, sizeof(n), 1, fp);
little
=
b
[
0
]
|
(
b
[
1
]
<<
8
)
;
// least-significant byte first
4uint16_t big = (b[0] << 8) | b[1]; // most-significant byte first
&&
fread
(
&
length
,
1
,
1
,
fp
)
==
1
)
3{
4 uint8_t value[256];
5 fread(value, 1, length, fp);
6 // decide what to do based on `tag`
7}