150

Hello I currently have a program that gets a full path of a file's location and is put into a variable that is the type of: boost::filesystem2::path

I have looked up how to do this and have found that using:

string result1 = boost::filesystem::basename (myPath)

will convert the path to string BUT it only converts the file name (e.g. if the path is "C:\name\bobsAwesomeWordDoc.docx" it just returns "bobsAwesomeWordDoc").

I have found the following on how to convert the entire path to string, but I don't know how to implement it in my program. I have tried multiple ways but I am getting conversion errors.

const std::string& string( ): This routine returns a copy of the string with which the path was initialized, with formatting per the path grammar rules.

(found here)

I have tried:

string result1 = string& (myPath);

and a few other variations.

6 Answers 6

202

You just need to call myPath.string().

Sign up to request clarification or add additional context in comments.

2 Comments

Are there guarantues that string returns utf8 encoded path on all platforms?
@SergeyShambir: Unfortunately not. One can explicitly use u8string(), but that doesn't help where string() is used automatically.
21

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.

5 Comments

The accepted answer is a much simpler alternative (and it works)
canonical is deprecated in newer versions of Boost boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/…
@BrianJack huh, how is it deprecated? Sry, cannot find any official note... see also 1.60 reference: boost.org/doc/libs/1_60_0/libs/filesystem/doc/…
@MarcoAlka See the linked table boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/… and note the second line: class path canonize() Function removed - that means it was once in the library but no longer--the definition of deprecated.
@BrianJack It is the canonize that is deprecated, per your second link, but canonical doesn't seem to be (per your first link, for 1_48 V3). Now in C++17 we have std::filesystem of course
3

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

Comments

2

Calling myPath.generic_string() will do what you need.

Comments

0

Personally I had to do

boost::filesystem::absolute(path).string()

to get it to work, as:

path.string()

kept returning a relative path.

Comments

-1

Do this

path.c_str();

You should be fine.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.