Help with file readers

in essence: i am trying to read a specific amount of bytes from a file.
i have been trying to update a library (rgbapng) to latest master but running into issues with how to use the file readers.

specifically, whenever i try to read into a buffer, when printing that buffer, it shows garbage data.

this is my latest frankenstein so far.

        const png_header: PngHeader = undefined;
        var read_buf: [25]u8 = @splat(0);
        var reader = file.reader(io, &read_buf);
        var allocating = std.Io.Writer.Allocating.init(std.heap.page_allocator);
        const writer = &allocating.writer;

        try reader.interface.streamExact(writer, 25);
        var buf = writer.buffered();
        std.log.debug("png header: {any}", .{@as(PngHeader, @bitCast(buf[0..25].*))});
        return png_header;

i also tried

    var reader = file.reader(io, &.{});
    try reader.interface.readSliceAll(buffer);

const png_header = try reader.interface.takeStruct(PngHeader, .little);
takeStruct does require the interface’s buffer be large enough.
I used .little endian as that is equivalent to your code, but it might be the source of your garbage as the data might be in .big endian order. It could also be a combination of big and little endian.

1 Like