🙆

【Zig】ファイルの読み書き

2024/05/03に公開
zig test main.zig
main.zig
# https://zig.guide/standard-library/filesystem/
              
const std = @import("std");
const expect = std.testing.expect;

test "createFile, write, seekTo, read" {
    const file = try std.fs.cwd().createFile(
        "test.txt",
        .{ .read = true },
    );
    defer file.close();

    const bytes_written = try file.writeAll("Hello File!");
    _ = bytes_written;

    var buffer: [100]u8 = undefined;
    try file.seekTo(0);
    const bytes_read = try file.readAll(&buffer);

    try expect(std.mem.eql(u8, buffer[0..bytes_read], "Hello File!"));
}

Discussion