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
If the image is in the current working directory of the program, you can use GetCurrentDirectory to get the absolute path to that directory.
A filesystem is a structure for the computer to store the files and folders that make up the data of the operating system. Inside a filesystem, folders are referred to as directories. Folders that exist inside other folders are called subdirectories.
To find the full absolute path of the current directory, use the pwd command.
The Boost Filesystem Library provides portable facilities to query and manipulate paths, files, and directories. The motivation for the library is the need to be able to perform portable script-like operations from within C++ programs.
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"
You can also use from this function.
std::cout << std::filesystem::path("/root/home/../opt/.").lexically_normal() << std::endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With