相同的语言环境,不同的输出

same locale, different output

本文关键字:输出 语言 环境      更新时间:2023-10-16

我尝试将UTF-8字符串打印到windows控制台。控制台代码页设置为65001 (utf-8),字体设置为lucida console, c++源文件编码为utf-8。考虑下面的代码:

#include<iostream>
#include<locale>
#include<clocale>
int main(int narg, char** arg){
    using namespace std;
    cout<<"C++ locale: "<<cout.getloc().name()
        <<"nC locale: "<<setlocale(LC_ALL, 0)<<"n";
    cout<<"中文n";
    printf("中文n");
    return 0;
}

输出为:

C++ locale: C
C locale: C
������
中文

谁能解释一下并给出一个解决方案(使c++和c有相同的正确输出)?非常感谢。

System: win7(32位)

编译器:vs2012 express

编辑:gcc在ubuntu12下的程序是正确的。

控制台默认不显示UTF,但是您可以使用:

chcp 65001将控制台更改为UTF或使用SetConsoleOutputCP通过代码更改它

希望这些有帮助:)附录:抱歉,一开始漏掉了这一点!唯一的方法,我可以得到?钻石符号出现是通过使用我的第二台机器没有国际字体。我不得不手动将控制台字体添加到注册表中,但是在windows控制台中显示utf字符集存在严重的问题。在我的windows 2003电脑上,我必须做以下操作:Start -> Control Panel -> Regional and Language Options -> Advanced -> Language for non-Unicode programs -> Chinese