#Understanding `std.Io.File.Reader` and `std.Io.File.Writer`

1 messages · Page 1 of 1 (latest)

finite isle
#

Hi. I'm new to Zig and low-level programming. I'm trying to learn the Io basics and using allocators through this simple code where I try to read a file and print it line by line. I'd appreciate any feedback on potential issues and tips to make it more idiomatic. Thanks in advance!

const std = @import("std");
const Io = std.Io;

pub fn main() !void {
    var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
    defer _ = debug_allocator.deinit();
    const gpa = debug_allocator.allocator();

    var threaded: Io.Threaded = .init(gpa, .{});
    defer threaded.deinit();
    const io = threaded.io();

    const file = try Io.Dir.cwd().openFile(io, "housing.csv", .{});
    defer file.close(io);

    var reader_buffer: [1024]u8 = undefined;
    var file_reader: Io.File.Reader = .init(file, io, &reader_buffer);

    var stdout_buffer: [1024]u8 = undefined;
    var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
    var stdout_writer = &stdout_file_writer.interface;

    var line_number: usize = 0;
    while (try file_reader.interface.takeDelimiter('\n')) |line| {
        try stdout_writer.print("line {d} --> {s}\n", .{ line_number, line });
        line_number += 1;
    }
    try stdout_writer.flush();
}
#

Zig version: 0.16.0-dev.1859+212968c57

magic swallow
#

for a small loop it doesn't matter much, but for larger while loops you can add a 'continue expression' which is executed every iteration, instead of putting it in the body. This is particularly useful if you use continue anywhere.

while(...) |...| : (line_number += 1) {...}

if you need to do multiple things in the continue expression you can use a block ({ ... })

#

you're not really doing anything to learn about allocators in this code.
if you are that new to zig I would recomend sticking to the latest 'stable' release which is 0.15.2.
if you specifically want to mess around with the new Io interface, thats understandable, but I still recomend learning the language on a stable release first. Though if you've gotten this far without much help you should be fine.

finite isle
#

Thanks for the feedback. Good reminder of the continue expression espesially if there are multiple lines.

I think originally I startd with the dev branch because it's what Ziglings expected. And yeah Io has been difficult to understand. I wouldn't be able to make it work by simply looking at the documentation if it wasn't for the template that zig init gives you.

As for the allocators, I don't know if it makes sense, but I'm planning to gradually build on top of the existing code by storing the data to an array or vector and ultimately train a regression model. Not the greatest learning path but gives me some hands-on exposure by doing something relevant to my field. I'm not sure if I can do it though based on how much time it took to make this code work.

magic swallow
# finite isle Thanks for the feedback. Good reminder of the continue expression espesially if ...

I startd with the dev branch because it's what Ziglings expected
ziglings has tagged the appropriate comits with their zig version, simply git checkout v0.15.1 to get a version that supports 0.15. note that the patch version might not match eg 0.15.2, it shouldnt cause any issues.

the best learning path is one that you are familiar with.

-# I expect someone in your field to constantly feel the pain of referring to growable arrays as vector, zig calls them ArrayList, like java.

#

-# I assume you meant growable arrays, since its in the context of allocators, zig does have @vector for simd, though the compiler will probably do a better job at that than you would manually so avoid them