How to convert boost path type to string?

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...      
}

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.

Tags:

C++

Boost

Path