17

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.

Emile Cormier
  • 26,080
  • 13
  • 87
  • 116
  • D'oh! I was looking for an API that would directly give me the path separator, and didn't think of simply using: `boost::filesystem::path("/").native()` – Emile Cormier Dec 05 '11 at 11:21
  • Can someone confirm that `boost::filesystem::path("/").native()` returns `"\\"` on Windows? – Emile Cormier Dec 05 '11 at 11:27
  • 3
    Just so you know, Windows accepts `/` as the pafh separator. – Xeo Dec 05 '11 at 11:30
  • @Xeo: Thanks, that renders my question moot for what I'm currently working on. But I'm still curious about the behavior of `boost::filesystem::path("/").native()` on Windows. – Emile Cormier Dec 05 '11 at 11:34
  • 2
    On WinXP, with VS2010, boost 1.48.0, the return value is "/". – fefe Dec 05 '11 at 12:05

3 Answers3

14

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();
Emile Cormier
  • 26,080
  • 13
  • 87
  • 116
  • New URL http://www.boost.org/doc/libs/release/libs/filesystem/doc/reference.html#path-make_preferred – KindDragon Jun 03 '13 at 13:44
  • On windows this will fail to compile, as native() will return std::wstring. That's irrelevant for the purposes of this question though. – AI0867 Apr 08 '14 at 15:50
  • @AI0867: Thanks, I replaced `std::string` with `bfs::path::string_type` in the example. – Emile Cormier Apr 10 '14 at 02:59
13

As of version 1.57, Boost now has a better solution, that is just constant char / wchar_t ( dependent on different platforms ): boost::filesystem::path::preferred_separator.

Read http://www.boost.org/doc/libs/release/libs/filesystem/doc/reference.html#Operating-system-examples for more information. There are even more system-dependent features in it.

Simple example:

#include <boost/filesystem.hpp>
#include <iostream>

int main() {
    std::cout << boost::filesystem::path::preferred_separator << std::endl;
}
Emile Cormier
  • 26,080
  • 13
  • 87
  • 116
fhntv24
  • 314
  • 2
  • 10
  • A quick look through the boost source reveals that boost just uses an ifdef to get the seperator during the preprocessor stage. In this instance there is no need to use boost but also, if they are on a non-standard filesystem on a *nix box then boost will incorrecly assume /. – tom Dec 21 '14 at 15:50
1

Haven't tested this, but it looks like you should be able to use this on a recent boost:

http://www.boost.org/doc/libs/1_43_0/libs/filesystem/doc/reference.html

#include <boost/filesystem.hpp>
#include <iostream>

int main() {
    std::cout << boost::filesystem::slash<boost::filesystem::path>::value << std::endl;
}
sehe
  • 328,274
  • 43
  • 416
  • 565