Dev*_*-il 6 c++ filepath c++-standard-library c++17 std-filesystem
我有这段代码
auto path = std::filesystem::path("/root/home/../opt/.");
Run Code Online (Sandbox Code Playgroud)
我曾尝试过, std::filesystem::absolute()但后来意识到这是因为我想要的结果之外的其他东西
我的问题是如何将该相对路径转换为绝对路径,以便结果是"/root/opt/".
我在 Debian g++-9 上使用 c++17
使用std::filesystem::canonical转路径与所有的绝对路径..移除(参考):
auto path = std::filesystem::canonical("/root/home/../opt/.");
Run Code Online (Sandbox Code Playgroud)
给你:
"/root/opt"
Run Code Online (Sandbox Code Playgroud)
您也可以使用此功能。
std::cout << std::filesystem::path("/root/home/../opt/.").lexically_normal() << std::endl;
Run Code Online (Sandbox Code Playgroud)