是否有方法使用Boost.Filesystem获得平台的路径分隔符字符?通过路径分隔符,我指的是Unix的/
和Windows的。
我已经知道我可以使用boost::filesystem::path::operator/
将两个路径与适当的分隔符连接在一起。但我只想要/
或。
我也知道我可以使用#ifdef _WIN32
,但我更喜欢Boost。文件系统告诉我合适的分隔符
编辑:我想使用Boost的第3版。文件系统API,在Boost 1.48中使用
从1.57版开始,Boost现在有了一个更好的解决方案,那就是恒定的char
/wchar_t
(取决于不同的平台):boost::filesystem::path::preferred_separator
.
阅读http://www.boost.org/doc/libs/release/libs/filesystem/doc/reference.html#Operating-system-examples获取更多信息。它甚至有更多系统相关的特性。
简单的例子:
#include <boost/filesystem.hpp>
#include <iostream>
int main() {
std::cout << boost::filesystem::path::preferred_separator << std::endl;
}
似乎boost::filesystem::path::make_preferred
是票:
Effects:包含的路径名被转换为首选的本地路径名格式。[注:在Windows上,效果是将斜杠替换为反斜杠。在POSIX上,没有影响。
的例子:
namespace bfs = boost::filesystem;
bfs::path slash("/");
bfs::path::string_type preferredSlash = slash.make_preferred().native();
还没有测试过,但看起来你应该能够在最近的一次提升中使用它:
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;
}