Buffered writer in zig 0.15.0-dev

Hi,
I am new to zig and experimenting the language. My use case is numerical and scientific computing
I want to try bufferedwriter in the standard library.

    const stdout_file = std.fs.File.stdout();
    var bw = std.io.bufferedWriter(stdout_file);
    const writer = bw.writer();

    timer.reset();
    for (0..N) |i| {
        try writer.print("{d}\n", .{i});
    }
    try bw.flush();

It doesn’t compile and gives the following error

C:\Program Files\zig\lib\std\Io\buffered_writer.zig:12:37: error: root source file struct 'fs.File' has no member named 'Error'
        pub const Error = WriterType.Error;
                          ~~~~~~~~~~^~~~~~

Can someone please point out the mistake in my code ?

Thank you,
Ashok

On the first line I think it should be

const stdout_file = std.fs.File.stdout().writer();

to return a Writer. But that’s only the case if you are using some version before std.Io: delete BufferedWriter by andrewrk · Pull Request #24743 · ziglang/zig · GitHub was merged.

Hi @ivelsantos ,
Thanks for your reply.
That PR was 3 days ago. I am using the version from the official website which says - 3rd August.
I used

const stdout_file = std.fs.File.stdout().writer();

But then i gives me this error

main.zig:20:45: error: member function expected 1 argument(s), found 0
    const stdout_file = std.fs.File.stdout().writer();
                        ~~~~~~~~~~~~~~~~~~~~^~~~~~~

Ok thanks for the pointer.
It works with

var stdout_buffer: [4096]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;
1 Like

sbipset:

var stdout = std.fs.File.stdout().writer(&.{});



inline fn Print( comptime format: []const u8, args: anytype) void {
   stdout.interface.print(format, args) catch  {} ;
}
inline fn WriteAll( args: anytype) void {
   stdout.interface.writeAll(args) catch {} ;
}

1 Like

FYI: std.io.bufferedWriter uses the old writer interface(s) you can use file.deprecatedWriter() to get a compatible interface, which can be passed to bufferedWriter.

The new interface(s) have buffers as part of the interface instead of an implementation as they were before.

1 Like

BufferedWriter is gone, by the way.

6 Likes

off-topic in terms of direction, but is there a similar thing (PR with some explanation etc.) for the Reader?

For instance, Io.GenericReader is deprecated, and I’m struggling a bit to find suitable replacements for its methods using the new Io.Reader.

1 Like