提问者:小点点

从std::filesystem::path C++获取绝对路径


我有这段代码

auto path = std::filesystem::path("/root/home/../opt/.");

我尝试了std::filesystem::absolute(),但后来意识到它不是为了我想要的结果

我的问题是如何将该相对路径转换为绝对路径,以便结果为“/root/opt/”

我在Debian g++-9上使用C++17


共2个答案

匿名用户

使用std::filesystem::canonical将路径转换为绝对路径,并删除所有..(参考):

auto path = std::filesystem::canonical("/root/home/../opt/.");

给你:

"/root/opt"

匿名用户

也可以使用from这个函数。

 std::cout << std::filesystem::path("/root/home/../opt/.").lexically_normal()    << std::endl;

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(std|filesystem|path|c++|获取|绝对路径)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?