Hi guys, Zig noob here, wondering how can/should I use the readFile from stdlib. The compiler lets me write the following, but it results on empty bytes and empty result, though:
You are almost there. You need to specify a slice with capacity as Zig won’t allocate for you. Or if you don’t know the size beforehand, use the readFileAlloc method with an allocator.
You also need to understand slices and arrays in Zig (see here).
Then if you do this, it will work:
// make the array writable by defining it as a var not a const
var bytes: [1<<16]u8 = undefined;
// use the array address to turn it into a slice
const result = try std.fs.cwd().readFile(filename, &bytes);
std.log.info("bytes: {any}", .{bytes});
std.log.info("result: {any}", .{result});