0Pricing
Zig Academy · Lesson

Reading Files and stdin

Stream input into your program.

Reading Files and stdin is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. 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 Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Tools Consume Input

A useful CLI usually reads something: a file you name, or text piped into it. Zig gives you clear, explicit ways to do both. 📥

Open a File by Path

Use std.fs.cwd to get the current directory, then openFile to open a path relative to it for reading.

const file = try std.fs.cwd().openFile(path, .{});

Always Close What You Open

An open file is a resource you own. Pair the open with a defer file.close() so it is released no matter how the function exits.

defer file.close();

Read the Whole File

To slurp everything at once, call readToEndAlloc with an allocator and a size limit. It returns the bytes as an owned slice.

const data = try file.readToEndAlloc(a, 1024 * 1024);

Free the Bytes Later

That returned slice lives on the heap, so you own it. Add defer a.free(data) to release the buffer when you are done with it.

defer a.free(data);

The Size Limit Is a Guard

That max-bytes argument protects you from a huge file eating all memory. Pick a limit that fits your tool's realistic input.

Grab Standard Input

To read piped text, reach for std.io.getStdIn. It hands back a file just like one you opened, so the same reading calls apply.

const stdin = std.io.getStdIn();

Read All of stdin

Ask stdin for a reader and call readAllAlloc. This collects everything piped in until end-of-input into one owned slice.

const input = try stdin.reader().readAllAlloc(a, 1024 * 1024);

Read Line by Line

For streaming work, a reader's readUntilDelimiterOrEof pulls one line at a time into a buffer, returning null when input ends.

const line = try r.readUntilDelimiterOrEof(&buf, '\n');

Buffer for Speed

Each raw read can be a system call. Wrap a reader in std.io.bufferedReader so many small reads batch into fewer, faster operations.

var br = std.io.bufferedReader(file.reader());

File or Pipe, Same Code

Because a file and stdin both expose a reader, your parsing logic does not care which one it got. That is clean, reusable design. ✨

Quick Check

Recall how you turn a file's bytes into something you can use.

Recap

You opened files with openFile, read them fully or line by line, pulled text from stdin, and freed every buffer. Input handled. 🎯

Frequently asked questions

Is the “Reading Files and stdin” lesson free?

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

What will I learn in “Reading Files and stdin”?

Stream input into your program. You practise Zig 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 Zig Academy?

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

How long does the “Reading Files and stdin” 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 Zig Academy lesson?

Yes. Every Zig 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. Reading Command-Line Arguments
  2. Reading Files and stdin
  3. Writing Output and Exit Codes
  4. Package and Release the Binary
← Back to Zig Academy