boost::filesystem - how to create a boost path from a windows path string on posix plattforms?

Posted by VolkA on Stack Overflow See other posts from Stack Overflow or by VolkA
Published on 2010-06-02T13:09:13Z Indexed on 2010/06/02 13:14 UTC
Read the original article Hit count: 178

Filed under:
|
|

I'm reading path names from a database which are stored as relative paths in Windows format, and try to create a boost::filesystem::path from them on a Unix system. What happens is that the constructor call interprets the whole string as the filename. I need the path to be converted to a correct Posix path as it will be used locally.

I didn't find any conversion functions in the boost::filesystem reference, nor through google. Am I just blind, is there an obvious solution? If not, how would you do this?

Example:

std::string win_path("foo\\bar\\asdf.xml");
std::string posix_path("foo/bar/asdf.xml");

// loops just once, as part is the whole win_path interpreted as a filename
boost::filesystem::path boost_path(win_path);
BOOST_FOREACH(boost::filesystem::path part, boost_path) {
    std::cout << part << std::endl;
}

// prints each path component separately
boost::filesystem::path boost_path_posix(posix_path);
BOOST_FOREACH(boost::filesystem::path part, boost_path_posix) {
    std::cout << part << std::endl;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost