Hi,
Allow me to start by saying that I’m by no means an inexperienced programmer. I’m coming to Zig from a C++ and Rust background. I’m really liking the way of thinking and how explicit all the code is, but I have to admit that IO is currently tripping me up a lot. I cannot work out for the life of me how to read the contents of a file into a buffer. I know from trying master that this is going to change even more in an upcoming Zig version, but this is with 0.15.2. This code panics, and I simply cannot get it to work no matter what reader methods I try it seems
Any help is most certainly appreciated, I’m certain I’m missing something small. My first ever experience with zig was me making a memory corruption in 10 lines by trying to return a local []const u8 from a function, but now I fully get how strings work and love them.
const std = @import("std");
pub fn main() !void {
const file = try std.fs.cwd().openFile("test.txt", .{ .mode = .read_only });
defer file.close();
var buffer: [1024]u8 = undefined;
const file_reader = file.reader(&buffer);
var reader = file_reader.interface;
try reader.readSliceAll(&buffer);
std.debug.print("{s}\n", .{buffer});
}
Thanks in advance!