kittikun kittikun - 2 months ago 12
C++ Question

boost filesystem canonical path is not valid after conversion to const char *

I am trying to transform a relative path and convert it to absolute to pass to SQLite using boost filesystem. This is supposed to work correctly for windows and linux

boost::filesystem::path path("../../data/dominion");
boost::filesystem::path file("dominion.db");
boost::filesystem::path canonical = boost::filesystem::canonical(dataPath / file);

canonical returns


As you can see the beginning of the path "D:/" is not correct. I also tried to call normalize() on it without success

Is there a way to remedy to this ?


Despite not being standard practice, forward slashes are also accepted on Windows, that's why boost isn't forcing the conversion.

However, some libraries won't accept forward slashes. boost::filesystem::path::make_preferred()[1] is meant to solve such situations, by converting the path to the system's preferred representation (i.e. using backslashes on Windows).

[1] This older reference makes that behaviour more obvious

As discused below, while (most of) the Windows API accepts forward slashes, and even a mixture of forward and backslashes, some user interfaces even on applications included in Windows don't.