6

Windows コンソールでの Unicode 文字の出力に問題があります。Windows XP と Code Blocks 12.11 を mingw32-g++ コンパイラと共に使用しています。

CまたはC++を使用してWindowsコンソールでUnicode文字を出力する適切な方法は何ですか?

これは私のC++コードです:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "šđč枊ĐČĆŽ" << endl; // doesn't work

    string s = "šđč枊ĐČĆŽ";
    cout << s << endl;            // doesn't work

    return 0;
}

前もって感謝します。:)

4

1 に答える 1

11

これらの文字のほとんどはエンコードに 1 バイト以上かかりますが、std::coutの現在組み込まれているロケールは ASCII 文字のみを出力します。そのため、出力ストリームに多くの奇妙な記号や疑問符が表示される可能性があります。std::wcoutこれらの文字は ASCII でサポートされていないため、UTF-8 を使用するロケールを吹き込む必要があります。

// <locale> is required for this code.

std::locale::global(std::locale("en_US.utf8"));
std::wcout.imbue(std::locale());

std::wstring s = L"šđč枊ĐČĆŽ";
std::wcout << s;

Windows システムの場合、次のコードが必要になります。

#include <iostream>
#include <string>
#include <fcntl.h>
#include <io.h>

int main()
{      
    _setmode(_fileno(stdout), _O_WTEXT);

    std::wstring s = L"šđč枊ĐČĆŽ";
    std::wcout << s;

    return 0;
}
于 2013-07-14T17:32:50.983 に答える