I am tempted to use `std.fs.Dir.realpath()`

All of these work fine for all of the Dir APIs, so, for example:

std.fs.cwd().openFile("./foo.bin", .{});
std.fs.cwd().openFile("../foo.bin", .{});
std.fs.cwd().openFile("foo.bin", .{});
std.fs.cwd().openFile("/home/Batman/downloads/foo.bin", .{});

all work. The relative paths are resolved relative to the Dir, which in the examples above is std.fs.cwd(), but it could be another directory if you want:

var dir = try std.fs.cwd().openDir("path/to/dir", .{});
defer dir.close();

const file = try dir.openFile("can-be-relative-or-absolute.bin", .{});
defer file.close();

If you want more details, I’ve made some other relevant comments in the past

5 Likes