【发布时间】:2015-05-26 09:46:10
【问题描述】:
我正在使用以下界面设置硬盘驱动器上文件的路径:
void setPath(const char* path);
此路径将用于基本文件 I/O。
例如,如果我提供一个包含中文字符的路径(例如,通过 QString::toUtf8()),这对 Unix 工作正常,但对于 Windows 当然不是,因为内部使用了 wchar/wstring API。
我现在正在寻找一种优雅的方式来使这个接口在基于 Windows 和 Unix 的系统上兼容 UTF-8。 有没有办法避免在基于 Windows 的系统上使用宽 API 并继续使用 std::string 和 std::ofstream() ?
在查看 boost::locale 之后,我觉得这可以处理 UTF-8 编码。这会是一种方法吗(例如,用它的 boost::ofstream() 对应物替换 std::ofstream ?)
const std::locale loc = generator.generate(std::locale(), "zh_CN.UTF-8");
std::locale::global(loc);
std::cout.imbue(std::locale());
boost::filesystem::path::imbue(std::locale())
感谢所有帮助。
【问题讨论】:
标签: file-io utf-8 portability boost-locale