Dev-il
Dev-il

Reputation: 167

Get the absolute path from std::filesystem::path c++

I have this piece of code

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

I had tried std::filesystem::absolute() but then realized it is for something else than the reasult I want

My question is how can i convert that relative path to the absolute path so that the reasult will be "/root/opt/".

I am using c++17 on Debian g++-9

Upvotes: 11

Views: 12582

Answers (2)

Farhad Sarvari
Farhad Sarvari

Reputation: 1081

You can also use from this function.

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

Upvotes: 7

Use std::filesystem::canonical to turn the path into an absolute path with all .. removed (reference):

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

Gives you:

"/root/opt"

Upvotes: 16

Related Questions