Strings, files and data formats in C
You handle text safely in C, read and write text and binary files, and build parsers that survive malformed input.
Level 201 · 20 lessons · 18 practice labs
Lessons
Strings
- string.h functions: strlen, strcmp, strchr, strstr
- Safe copying and joining: snprintf and strlcpy
- Splitting text: strtok_r and strsep
- Number conversion: strtol instead of atoi, with error checks
- Character classes: ctype.h and UTF-8 bytes
- Short task: count the words in a text
Streams and files
- stdio buffering: fflush, setvbuf and stderr
- Reading long lines safely: getline
- Binary files: fread, fwrite, fseek and ftell
- Walking directories: opendir and readdir
- Temporary files: mkstemp and safe permissions
- Short task: a file copier with error checks
Formats and parsing
- Struct layout: alignment, padding and packed
- Byte order: htonl, ntohl and portable reading
- Parsing CSV: quotes and malformed lines
- A key=value configuration file reader
- Reading a binary file header: magic numbers and length fields
- Writing your own hex dump tool
- Short task: read the dimensions of a BMP file
Capstone
- Capstone: a record-file parser that survives malformed input
Practice labs
- Splitting a line into fields with strtok_r: Split a semicolon-separated record line into fields with strtok_r and return the field count.
- Validating a port number with strtol: Replace an atoi-based port reader with strtol, endptr and errno checks so "80abc" and "99999" are rejected.
- Joining tags in a bounded buffer: Write a function that joins a tag list with commas, never exceeding the buffer and reporting truncation.
- Counting characters in UTF-8 text: Write a function that returns the number of UTF-8 characters rather than bytes and errors on malformed sequences.
- Finding the longest line with getline: Find the longest line in a file and its number with getline, with no line length limit.
- Jumping to a record in a binary file: Read the n-th record of a file of fixed-size binary records with fseek and reject requests beyond the file.
- Saving a settings file with mkstemp and no half-writes: Write a save function that writes settings to a mkstemp temp file and swaps it in with rename, with 0600 permissions.
- Counting extensions in a directory: Count files in a folder by extension with opendir and readdir, skipping the . and .. entries.
- Reading a packet header in network byte order: Write a function that portably reads a 32-bit length and a 16-bit type field from a byte array.
- Reducing padding bytes in a struct: Shrink a sensor record struct by reordering its fields and test the layout with offsetof.
- Writing a struct to a file portably: Turn code that fwrites a raw struct into field-by-field little-endian serialisation and test reading it back.
- Splitting quoted CSV fields: Write a CSV line parser that correctly splits quoted fields containing commas.
- Reading a key=value file with comments: Complete a settings reader that handles comment lines, whitespace and repeated keys correctly.
- Reporting malformed CSV lines: Write a parser that skips lines with unclosed quotes or missing fields and reports them with line numbers.
- Reading the sample rate from a WAV header: Verify a WAV file's magic bytes and print its sample rate and channel count.
- Not trusting a length field blindly: Add bounds checks to a reader that never compares the header's length field with the file size, rejecting truncated files.
- Showing a hex diff of two binary files: Write a tool that compares two binary files and lists the offsets and values of differing bytes in hex.
- Joining paths without overflow during a directory walk: Make path joining in a recursive directory walker overflow-free in a fixed buffer, count the paths that do not fit and pass the deep-tree tests.
About this course
You handle text safely in C, read and write text and binary files, and build parsers that survive malformed input.
What you will learn
- Strings
- Streams and files
- Formats and parsing
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.