【发布时间】:2016-12-23 23:40:14
【问题描述】:
我正在将std::wclog 重定向到一个文件以登录我的程序:
std::wclog.rdbuf((new std::wofstream("C:\\path\\to\\file.log", std::ios::app))->rdbuf());
通过写信给std::wclog:
std::wclog << "Schöne Grüße!" << std::endl;
令人惊讶的是,我发现该文件是用 ANSI 编写的。 (这对于ofstream 和clog 来说是完全可以接受的,但我曾预计wofstream 和wclog 会产生某种unicode 输出。)我也希望能够登录CYK 语言(例如用户输入),那么有没有办法让wofstream 生成 UTF-8? openmode 标志似乎没有提供这一点。
(如果没有平台无关的方式,我是Win7+ 64位的。)
编辑:
上面的问题有错误。线
std::wclog << "Schöne Grüße!" << std::endl;
应该是
std::wclog << L"Schöne Grüße!" << std::endl;
这只是为了演示我想要做什么,在现实生活中,wstring 被写入wofstream 来自一个提供翻译的类,比如
std::wclog << _(L"Best regards") << std::endl;
在哪里
#define _(X) i18n::translate(X)
class i18n {
public:
static std::wstring translate(const std::wstring&);
}
所以我想做的是使用wofstring 将wstring 写入std::wclog 将其放入文件中,并且该文件应该是UTF-8 编码(没有BOM)。
【问题讨论】:
-
为什么要将窄字符写入宽字符流?
-
我想如果你想要的话,你需要使用 UTF 文字吗?那么语言环境呢?
-
您需要为 Unicode 使用正确的类型和文字。 Visual C++ supports the C++11 Unicode 文字和类型。例如,`u8"hello"` 是 UTF-8 编码的
char*,u"hello"是char16_t*,而u8"hello"s和u"hello"s返回std::string和std::u16string。一般来说,最好使用 STL 字符串类型
标签: c++ logging utf-8 widechar