To convert a std::filesystem::path to a natively-encoded string (whose type is std::filesystem::path::value_type ), use the string() method. Note the other *string() methods, which enable you to obtain strings of a specific encoding (e.g. u8string() for an UTF-8 string).
The Boost Filesystem Library provides portable facilities to query and manipulate paths, files, and directories. The motivation for the library is the need to be able to perform portable script-like operations from within C++ programs.
You just need to call myPath.string()
.
I believe you need to do a little more than just convert the path to a string - you should first obtain the canonical version of the path - an absolute path with no symbolic-link elements - and convert that into a string:
boost::filesystem::canonical(myPath).string();
P.S. - I've been programming with Boost for ages and I couldn't easily find this info in the docs.
Update (Oct 2017)
Documentation: boost::filesystem::canonical.
But note that as of C++17 there is std::filesystem, with canonical and a lot more.
This worked in wxWidgets: (I know I should just use the wx utilities but it is a test)
void WxWidgetsBoostTestFrame::OnTestBtnClick(wxCommandEvent& event)
{
boost::filesystem::path currentPath;
currentPath = boost::filesystem::current_path();
std::string curDirString;
curDirString = boost::filesystem::canonical(currentPath).string();
wxString mystring(curDirString.c_str(), wxConvUTF8);
wxMessageBox(mystring); // output: C:/Users\client\Desktop...
}
Calling myPath.generic_string()
will do what you need.
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