Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::filesystem get relative path

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.

like image 868
itun Avatar asked Apr 16 '12 00:04

itun


People also ask

How do you convert an absolute path to a relative path?

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.

What is relative path and 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) ...

What is a relative path?

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.


1 Answers

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; } 
like image 96
milaniez Avatar answered Sep 23 '22 23:09

milaniez