C++ std::wofstream unicode issue.



我使用VS 2012编写了以下代码:

std::wofstream logout("my_log.txt", std::ios_base::out | std::ios_base::trunc);
std::locale utf8_locale(locale(), new codecvt_utf8<wchar_t>);
logout.imbue(utf8_locale);
if (!logout.is_open()) 
{
 printf("Cannot open file.n"); 
 return 1; 
}
else printf("Log file created.n");
logout << "Client IP            │"<< "Recv time                │"<< "Request               │"<< "Response     "<<endl;
logout << "─────────────────────┼"<< "─────────────────────────┼"<<endl;

在my_log.txt文件中,所有unicode符号都被替换为"?????"。我想创建类似于日志文件表的东西。如果我使用像"——"这样的标准ASCII符号,它将工作,并且它们都被精确地显示出来。

这就是为什么我们在字符串字面量中只使用ASCII的原因。

你的代码很好,但是你的字符串字面值显然不包含你认为它们应该包含的内容。这可能是由于错误配置或功能不足的文本编辑器。

使用转义序列代替(uXXXX),这样你就知道你得到了什么。在2015年,用ASCII字符组成的源代码是不会出错的。

哦,这其实很有趣。

wchar_t *s1 = L"u2502"; // "│"
wchar_t *s2 = L"u2500"; // "─"
wchar_t *s3 = L"u253C"; // "┼"
logout << s1<<s2<<s3<<endl;

在我的。txt文件现在我看到unicode数字。但我可以使用相同的结构没有(uXXXX)

logout << L" │ ─ ┼"<<endl;

最新更新