嗨,我试图将unicode字符串输出到带有iostreams的控制台并失败.
我发现了这一点: 在c ++控制台应用程序中使用unicode字体 ,这个代码片段有效.
SetConsoleOutputCP(CP_UTF8);
wchar_t s[] = L"èéøÞ????æ?a";
int bufferSize = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL);
char* m = new char[bufferSize];
WideCharToMultiByte(CP_UTF8, 0, s, -1, m, bufferSize, NULL, NULL);
wprintf(L"%S", m);
Run Code Online (Sandbox Code Playgroud)
但是,我没有找到任何方法来使用iostream正确输出unicode.有什么建议?
这不起作用:
SetConsoleOutputCP(CP_UTF8);
utf8_locale = locale(old_locale,new boost::program_options::detail::utf8_codecvt_facet());
wcout.imbue(utf8_locale);
wcout << L"¡Hola!" << endl;
Run Code Online (Sandbox Code Playgroud)
编辑 我找不到任何其他解决方案,而不是在流中包装此片段.希望,有人有更好的想法.
//Unicode output for a Windows console
ostream &operator-(ostream &stream, const wchar_t *s)
{
int bufSize = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL);
char *buf = …Run Code Online (Sandbox Code Playgroud) 例:
#include <iostream>
using namespace std;
int main()
{
wchar_t en[] = L"Hello";
wchar_t ru[] = L"??????"; //Russian language
cout << ru
<< endl
<< en;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码仅打印像地址一样的十六进制值.如何打印wchar_t字符串?
无法绕过这一个是一个真正的耻辱源...
我在法语Windows(XP)中使用法语版的Visual Studio(2008).发送到输出窗口的字符串中的法语重音被破坏.从输出窗口输入Ditto .典型的字符编码问题,我输入ANSI,得到UTF-8作为回报,或者那样的东西.在向输出窗口显示"硬编码"字符串时,什么设置可以确保字符保留在ANSI中?
编辑:
例:
#include <iostream>
int main()
{
std:: cout << "àéêù" << std:: endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
将在输出中显示:
奥羽
(此处编码为HTML以供您观看乐趣)
我真的很想表明:
àéêù
我知道你不应该将打印与printf,cout和wprintf,wcout混合,但很难找到一个好的答案为什么以及是否有可能绕过它.问题是我使用一个外部库,用printf打印,我自己使用wcout.如果我做一个简单的例子它工作正常,但从我的完整应用程序它只是不打印printf语句.如果这确实是一个限制,那么会有许多库无法与广泛的打印应用程序一起工作.对此的任何见解都非常受欢迎.
更新:
我把它归结为:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <readline/readline.h>
#include <readline/history.h>
int main()
{
char *buf;
std::wcout << std::endl; /* ADDING THIS LINE MAKES PRINTF VANISH!!! */
rl_bind_key('\t',rl_abort);//disable auto-complete
while((buf = readline("my-command : "))!=NULL)
{
if (strcmp(buf,"quit")==0)
break;
std::wcout<<buf<< std::endl;
if (buf[0]!=0)
add_history(buf);
}
free(buf);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以我想这可能是一个冲动的问题,但它看起来仍然很奇怪,我必须检查它.
更新 - >解决方法:
首先,wprintf出现同样的问题.但我发现添加:
std::ios::sync_with_stdio(false);
Run Code Online (Sandbox Code Playgroud)
实际上做了诀窍......(注意错误而不是我所期望的那样......),唯一困扰我的是,我不明白为什么以及如何弄明白:-(
考虑以下代码:
#include <iostream>
using namespace std;
class X {
public:
operator const wchar_t* () const { return L"Hello"; }
};
void f(const void *) {
wcout << L"f(const void*)\n";
}
void f(const wchar_t*) {
wcout << L"f(const wchar_t*)\n";
}
int main() {
X x;
f(x);
wcout << x;
}
Run Code Online (Sandbox Code Playgroud)
输出是(使用VS2015 C++编译器编译):
Run Code Online (Sandbox Code Playgroud)f(const wchar_t*) 00118B30
因此,似乎编译器选择了预期的 const wchar_t*重载f(因为存在从内X到的隐式转换const wchar_t*).
但是,它似乎wcout << x选择了const void*重载,而不是const wchar_t*一个(打印地址,而不是wchar_t字符串).
为什么是这样? …
使用g ++进行编译后,下面的程序std::wcout仅显示表达式。但是,如果您取消注释第八行,它将正确打印三个表达式。
我想知道这种奇怪行为的原因。
#include <iostream>
#include <cstring>
#include <boost/format.hpp>
int main () {
int x = 10;
wchar_t str[] = L"Hello, world!";
// std::cout << "what?" << std::endl;
std::wcout << L"str = \"" << str << L"\" | len = " << wcslen(str) << L"\n";
std::cout << boost::format("x = %d | &x = %p") % x % &x << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)