#Read entire content of a fs.File (stdout of a child process) to a buffer

1 messages · Page 1 of 1 (latest)

midnight cedar
#

Hi! This is probably a very basic question but I'm having trouble with it. I'm using a current zig 0.16 version (0.16.0-dev.1316+181b25ce4) and I'm having a bit of trouble understanding the new Io interface. I want to read the whole content of a file to a buffer. I'm using this code:

var alloc: std.mem.Allocator = // initialized elsewhere
var myFile: std.fs.File = // initialized elsewhere

var staticBuffer: [4096]u8 = undefined;
var reader = myFile.reader(io, &staticBuffer);
const buf = try reader.interface.readAlloc(alloc, std.math.maxInt(usize));
defer alloc.free(buf);

However, when I run this snipped, the assignment to buf in the 2nd last line returns with an OutOfMemory error. If I increase the std.math.maxInt(usize) to something smaller, I get an EndOfStream error. What would be the correct approach to read the whole content of the file? In my case, the file is the stdout of a subprocess and I cannot figure it out.

midnight cedar
#

The fact that my file is the stdout of a child process is probably very important here. Is there an ideomatic function that reads the whole stdout of a finished child process to a buffer?

#

Read entire content of a fs.File (stdout of a child process) to a buffer