Obtain platform's path separator using Boost.Filesystem

Emile Cormier picture Emile Cormier · Dec 5, 2011 · Viewed 18.9k times · Source

Is there a way to obtain the platform's path separator character using Boost.Filesystem? By path separator, I mean / for Unix and \ for Windows.

I already know I can use boost::filesystem::path::operator/ to concatenate two paths together with the appropriate separator character. But I just want either / or \.

I also know I can use #ifdef _WIN32, but I'd prefer that Boost.Filesystem tell me the appropriate separator character.

EDIT: I want to use version 3 of the Boost.Filesystem API, as used in Boost 1.48.

Answer

Emile Cormier picture Emile Cormier · Dec 5, 2011

It seems like boost::filesystem::path::make_preferred is the ticket:

Effects: The contained pathname is converted to the preferred native format. [Note: On Windows, the effect is to replace slashes with backslashes. On POSIX, there is no effect. -- end note]

Example:

namespace bfs = boost::filesystem;
bfs::path slash("/");
bfs::path::string_type preferredSlash = slash.make_preferred().native();