Fail to open POSIX file

Because the path you have provided contains the null character, which has the numeric code of 0.
You can see the assertion second from the top of the stack trace.

This is because bufPrint inserts at the start of the buffer and doesn’t touch the rest of it.
And you are passing the entire buffer to createFile.

Simply use the success result of bufPrint it’s a slice that points to just the part of the buffer that contains what it printed.

Your catch on bufPrint is also highly questionable, I would recommend one of the following:

pub fn dump_prg(self: Self) !void {
        const bufferSize = 100;
        var path_buf: [bufferSize]u8 = [_]u8{0} ** bufferSize;
        const path = std.fmt.bufPrint(&path_buf, "{s}.prg", .{self.name}) catch "name_too_long.prg";
    //...
}

or

pub fn dump_prg(self: Self) !void {
    const bufferSize = 100;
    var path_buf: [bufferSize]u8 = [_]u8{0} ** bufferSize;
    // truncate the name so it's gaurunteed to fit in the buffer
    const name = self.name[0..@min(self.name.len, bufferSize - 4)];
    const path = std.fmt.bufPrint(&path_buf, "{s}.prg", .{self.name}) catch unreachable;
    //...
}
4 Likes