#How to read a binary file to memory?

1 messages · Page 1 of 1 (latest)

open ruin
#

I want to read a binary file into a byte array, ideally in one go. how to?

#

got it

const file = try std.fs.cwd().openFile("testfile", .{});
defer file.close();

const allocator = std.heap.page_allocator;
const file_size = try file.getEndPos();
const buffer = try allocator.alloc(u8, file_size);
_ = try file.readAll(buffer);