How to convert boost path type to string?

Johnston picture Johnston · Nov 14, 2010 · Viewed 84.1k times · Source

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.

Answer

icecrime picture icecrime · Nov 14, 2010

You just need to call myPath.string().