Skip to content

Reading files in the new API #25033

Description

@yobibyte

Zig Version

0.15.1

This is the zig-x86_64-linux-0.15.1.tar.xz version.

Steps to Reproduce and Observed Behavior

~/dev/y % cat test.txt 
Hello, world!
~/dev/y % cat 1.zig 
const std = @import("std");
pub fn main() !void {
    const file = try std.fs.cwd().openFile("test.txt", .{});
    defer file.close();
    var buffer: [1024]u8 = undefined;
    var reader = file.reader(&buffer);
    const line = try reader.interface.takeDelimiterExclusive('\n');
    std.debug.print("{s}\n", .{line});
}

~/dev/y % cat 2.zig 
const std = @import("std");
pub fn main() !void {
    const file = try std.fs.cwd().openFile("test.txt", .{});
    defer file.close();
    var buffer: [1024]u8 = undefined;
    var reader = file.reader(&buffer).interface;
    const line = try reader.takeDelimiterExclusive('\n');
    std.debug.print("{s}\n", .{line});
}

I have two zig files as above. They differ by whether keeping .interface a part of the reader variable, or calling it when actually using the reader.

Running zig run 1.zig works as intended. It reads the file and shows the output. However, if I run zig run 2.zig, I get the following error:

~/dev/y % zig run 1.zig 
Hello, world!
~/dev/y % zig run 2.zig 
error: ReadFailed
/home/yobibyte/src/zig-x86_64-linux-0.15.1/lib/std/posix.zig:891:22: 0x1091275 in read (std.zig)
            .BADF => return error.NotOpenForReading, // Can be a race condition.
                     ^
/home/yobibyte/src/zig-x86_64-linux-0.15.1/lib/std/fs/File.zig:852:5: 0x105fa9b in read (std.zig)
    return posix.read(self.handle, buffer);
    ^
/home/yobibyte/src/zig-x86_64-linux-0.15.1/lib/std/fs/File.zig:1526:13: 0x1145bb0 in readStreaming (std.zig)
            return error.ReadFailed;
            ^
/home/yobibyte/src/zig-x86_64-linux-0.15.1/lib/std/fs/File.zig:1331:27: 0x1142a18 in stream (std.zig)
                const n = try readStreaming(r, dest);
                          ^
/home/yobibyte/src/zig-x86_64-linux-0.15.1/lib/std/Io/Reader.zig:776:25: 0x1141123 in peekDelimiterInclusive (std.zig)
            else => |e| return e,
                        ^
/home/yobibyte/src/zig-x86_64-linux-0.15.1/lib/std/Io/Reader.zig:811:21: 0x113f136 in takeDelimiterExclusive (std.zig)
        else => |e| return e,
                    ^
/home/yobibyte/dev/y/2.zig:7:18: 0x113d573 in main (2.zig)
    const line = try reader.takeDelimiterExclusive('\n');
                 ^

Is this a bug or my misunderstanding? If this is the latter, are there any docs/comments in the code I could read to understand it better?

Thanks!

Expected Behavior

I would assume that both examples produce the same output.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionNo questions on the issue tracker, please.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions