#read file

1 messages · Page 1 of 1 (latest)

hearty lava
#

how do i read file ?
it seems this code from 0.9 isnt working in 0.10

#
const AsmFile = struct {
    name: []const u8 = undefined,
    contents: [*:0]const u8 = undefined,
    length: u32 = 0,
};

pub fn readFile(
    name: []const u8,
    allocator: std.mem.Allocator,
) !AsmFile {
    var path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
    const abs_path = try std.fs.realpath(name, &path_buffer);
    var file = try std.fs.openFileAbsolute(abs_path, .{ .read = true });
    defer file.close();

    const file_size = try file.getEndPos();
    if (file_size > max_32bit) return error.FileTooBig;

    const buffer = try allocator.allocSentinel(u8, file_size, 0);

    _ = try file.read(buffer);

    return AsmFile{
        .file_name = name,
        .contents = buffer,
        .length = @truncate(u32, file_size),
    };
}
#

it still shows the error

surreal robin
#

just remove .read = true?

#

it's saying that the field read doesn't exist

#

@hearty lava