32

My current working directory is located at /home/myuser/program, I created a boost::filesystem::path object pointing to it. I appended /../somedir so it becomes /home/myuser/program/../somedir. But I need to get its resolved absolute path, which would be /home/myuser/somedir.

I have been trying for long time and I do not find any method in their reference to do this. There is a method called make_absolute, which seems to be supposed to do what I expect, but I have to give it a “root” path argument. Which should it be? do I really need to do this to get the real absolute path? Is there any other way?

0

5 Answers 5

25

You say you want an absolute path, but your example shows that you already have an absolute path. The process of removing the .. components of a path is known as canonicalization. For that, you should call canonical. It happens to also perform the task of absolute, so you don't need to call absolute or make_absolute first. The make_absolute function requires a base path; you can pass it current_path() if you don't have anything better.

2
  • Exatcly. I did not want to use the word “canonical” since I was not sure if it also meant resolved symlinks and such, for example. I tried your suggestion and it worked immediately. Thanks.
    – user1598585
    Sep 28, 2012 at 17:01
  • 6
    Note that canonical throws exception if file do not exists. So you could construct only existing path. Jan 22, 2013 at 12:55
14

Update, since this still appears to be Google's top hit concerning absolute paths:

As of Boost 1.57, some of the previously suggested functions have since been removed.

The solution that worked for me was

boost::filesystem::path canonicalPath = boost::filesystem::canonical(previousPath, relativeTo);

(using the free-standing method canonical(), defined in boost/filesystem/operations.hpp, which is automatically included via boost/filesystem.hpp)

Important: calling canonical on a path that does not exist (e.g., you want to create a file) will throw an exception. In that case, your next best bet is probably boost::filesystem::absolute(). It will also work for non-existing paths, but won't get rid of dots in the middle of the path (as in a/b/c/../../d.txt). Note: Make sure relativeTo refers to a directory, calling parent_path() on paths referring to files (e.g. the opened file that contained a directory or file path relative to itself).

2
  • What is an example path string of "previousPath" and "relativeTo"? I can't quite figure out what the input needs to look like in terms of path style. Nov 26, 2015 at 17:30
  • Previous path could something like path("../../readme.txt"), with relativeTo = path("c:/example/test/blah") this should then resolve to "c:/example/readme.txt". Not sure what the relativeTo does (or if a sensible input is even required), if previous path is already absolute.
    – Daniel
    Dec 1, 2015 at 6:52
1

The documentation shows that the make_absolute has an optional second parameter that defaults to your current path:

path absolute(const path& p, const path& base=current_path());

Try it without the second parameter and see if it returns the results you're looking for.

1
  • That is one of the inconsistencies that annoyed me a little, in the docs. There is a make_absolute method that has one parameter, when I follow the link, it directs me to the documentation of an absolute() function which accepts two parameters. Still, doing it like absolute(pathObject).string() will return the same unresolved path. This is what you suggested, right?
    – user1598585
    Sep 28, 2012 at 16:58
0

I have to give it a “root” path argument.

Check the docs: you don't have to give it anything; it has a default second parameter. Namely, the current directory.

Relative paths are relative to some directory. Thus, when making a path absolute, you need to know what it should be absolute relative to. That's the "root path": the directory it is relative to.

0
// input: d:\\tmp\\\\a/../VsDebugConsole.png
// output: d:\\tmp\\VsDebugConsole.png
static std::wstring fix_path(std::wstring path)
{
    //boost::replace_all(path, L"\\\\", L"\\");
    //boost::replace_all(path, L"//", L"/");
    boost::filesystem::path bpath(path);
    bpath = boost::filesystem::system_complete(bpath);

    return bpath.wstring();
}
1
  • 1
    Could you add a little more explanation?
    – Paul Floyd
    Jan 15, 2021 at 9:01

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.