【发布时间】:2018-03-05 22:32:24
【问题描述】:
我遇到了boost文件流的问题:我需要在windows下的用户目录中创建和修改文件。但是,用户名包含一个变音符号,这在 MinGW 下编译时会失败,因为标准缺少用于 boost 使用的文件流的 wide_char open() API。见Read/Write file with unicode file name with plain C++/Boost、UTF-8-compliant IOstreams和https://svn.boost.org/trac10/ticket/9968
但是我遇到了问题,这个问题主要发生在尝试使用系统代码页之外的字符时。在我的情况下,我只使用系统代码页中的字符,因为显然用户目录存在。这让我想,如果我可以告诉 boost::path 期望所有 std::strings 都作为 UTF8 编码但在调用 string() 成员函数时将它们转换为系统编码(这发生在 boost::fstream::open )
所以基本上:有没有办法使用 boost(和 boost Locale)自动进行转换(UTF8->系统编码)?
这里要完整的是我设置语言环境的代码:
#ifdef _WIN32
// On windows we want to enforce the encoding (mostly UTF8). Also using "" would use the default which uses "wrong" separators
std::locale::global(boost::locale::generator().generate("C"));
#else
// In linux / OSX this suffices
std::locale::global(std::locale::classic());
#endif // _WIN32
// Use also the encoding (mostly UTF8) for bfs paths
bfs::path::imbue(std::locale());
【问题讨论】: