Reading data from file/stdin in chunks

I want to write my own hexdump to learn Zig. I need to read data from stdin and/or a some file.

I guess the correct way to avoid allocating memory while reading the whole data at once, this mean that I need to read it in chunks and process them one by one. The whole process should stop when all of the data is read (for example stop when EOF is in the chunk).

Currently I am stuck with chunking stuff, what is the correct way to read and then flush the chunk, and how to stop it when the EOF is read?

Use readAll to read into a slice. If the length returned by readAll is less than the length of the buffer, then you’ll have reached EOF. If your file is exactly a multiple of your buffer size, then the final return value of readAll will be zero, otherwise file.len % buf.len.

3 Likes

You could also just use a buffered reader to handle all the chunking logic for you.

2 Likes

I ended up using both advices, works just as I have imagined. Thanks everybody!

    var in = std.io.getStdIn();
    if (fileNamePointer.len != 0) {
        const cwd = std.fs.cwd();
        in = try cwd.openFile(fileNamePointer, .{});
    }
    defer in.close();

    var bufRead = std.io.bufferedReader(in.reader());

    const buffer = try allocator.alloc(u8, lineStop);

    printHeader(lineStop, wordLength);

    var offset: i64 = 0;

    while (true) {
        const len = try bufRead.reader().readAll(buffer);

        if (len == 0) break;