What methods of the boost::filesystem
library can help me to get a path relative to another path?
I have a path /home/user1/Downloads/Books
and a path /home/user1/
. Now I want to get a path Downloads/Books
.
The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.
An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path. Relative path is defined as the path related to the present working directly(pwd) ...
A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.
In new versions of boost
(starting in 1.60), you can use boost::filesystem::relative
. (See the documentation here.)
#include <boost/filesystem.hpp> #include <iostream> namespace fs = boost::filesystem; int main() { fs::path parentPath("/home/user1/"); fs::path childPath("/home/user1/Downloads/Books"); fs::path relativePath = fs::relative(childPath, parentPath); std::cout << relativePath << 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