如何在 Zig 中将文件读取到字节数组?
我想将文件的内容读取到 Zig 中的字节数组。遗嘱内容适合记忆。
这首先获取文件的大小并将其用作
max_size
的 readToEndAlloc
。这假设文件的大小保持不变,也就是说,在运行此代码时它不会改变。
const file = try std.fs.cwd().openFile(path, .{});
defer file.close();
const stat = try file.stat();
const buf: []u8 = try file.readToEndAlloc(allocator, stat.size);
于 2024 年 12 月使用 Zig 0.13.0 进行测试。