如何获取`std.fs.Dir`的完整路径?

问题描述 投票:0回答:1

有没有办法访问

std.fs.Dir
结构的完整路径?我已经查看了源代码中的所有方法,但找不到任何获取目录上路径相关信息的内容。

zig
1个回答
4
投票

一个选项是

realpath

std.fs.Dir.realpathAlloc(self: Dir, 分配器: 分配器, 路径名: []const u8) ![]u8

const std = @import("std");

pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    const alloc = arena.allocator();

    std.log.info("cwd: {s}", .{
        try std.fs.cwd().realpathAlloc(alloc, "."),
    });
}

realpath 内部调用

std.os.getFdPath
,而 Dir 上似乎没有任何函数可以立即执行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.