18

有没有办法使用Boost.Filesystem获取平台的路径分隔符?路径分隔符是指/Unix 和\Windows。

我已经知道我可以使用boost::filesystem::path::operator/适当的分隔符将两条路径连接在一起。但我只想要/or \

我也知道我可以使用#ifdef _WIN32,但我更希望 Boost.Filesystem 告诉我适当的分隔符。

编辑:我想使用Boost.Filesystem API 的第 3 版,如 Boost 1.48 中使用的那样。

4

3 回答 3

15

从 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;
}
于 2014-12-19T09:40:00.613 回答
14

好像boost::filesystem::path::make_preferred是票:

效果:包含的路径名被转换为首选的本机格式。[注意:在 Windows 上,效果是将斜杠替换为反斜杠。在 POSIX 上,没有效果。——结束注]

例子:

namespace bfs = boost::filesystem;
bfs::path slash("/");
bfs::path::string_type preferredSlash = slash.make_preferred().native();
于 2011-12-05T13:54:36.750 回答
1

尚未对此进行测试,但看起来您应该能够在最近的提升中使用它:

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;
}
于 2011-12-05T13:07:51.400 回答