Question

I'm using Boost::Filesystem to traverse around directories in Linux.

Every time I need to re-define the path to be one directory back, I do something similar to this:

auto p = boost::filesystem::current_path();
p /= "../";

The problem is, that when I output 'p', it will show me the path with "../" still tacked on. How do I get this evaluated each time I decide to go back a directory. I would like going back a directory to make the path shorter- instead of making the path longer and longer every time.

I thought one of these functions might do it, as they take a path by reference,
boost::filesystem::absolute(...)
boost::filesystem::canonical(...)
but after calling them and re-outputting 'p', the result still shows a "../";

path& make_preferred() does not work either.

Was it helpful?

Solution

canonical or absolute is the way to do it. Do you use the returned path? The given path is taken as a constant reference so it is not modified in place. From the boost manuals:

path canonical(const path& p, const path& base = current_path());
path canonical(const path& p, system::error_code& ec);
path canonical(const path& p, const path& base, system::error_code& ec);

Overview: Converts p, which must exist, to an absolute path that has no symbolic link, dot, or dot-dot elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top