わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

標準出力にユニコードを出力させる

Windowsでコンソールアプリケーションを作成した際に、wprintfやwcoutでユニコードを表示させても標準出力に何も表示されません。 表示させるようにする為には、setlocale()関数に"LC_CTYPE", ""を指定して呼び出すと、ユニコードの日本語の文字列も標準出力に表示されるようになります。(Visual Studio 2008)

標準出力にユニコードを出力させる
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <locale.h>
 
 
 
int _tmain
(
      int argc
    , _TCHAR* argv[]
)
{
    /*
        ロケールを日本に設定
         これを設定するだけで、標準出力に日本語が表示される
         ようになります。
    */
    setlocale( LC_ALL, "Japanese" );
 
    // 文字列
    std::wstring str = L"あいうえお¥n";
 
    // 標準出力へ出力する
    std::wcout << str << std::endl;
 
    // 正常終了
    return( 0 );
}

実行結果

あいうえお

Visual Studio 2015の場合

Visual Studio 2015の場合は、setlocale(LC_CTYPE,"")を行うことで、wprintfでユニコードが表示されるようになりますが、 std::coutでShift-JISコードが出なくなります。wcoutでユニコードを表示する為には、wcoutのimbueを呼び出す必要があります。

現在のところ、printf, wprintf, cout, wcoutの全てのケースでユニコードを表示可能にする方法が見つけられていません。 (ご存じの方がいらっしゃいましたら、ご教授ください)

wcoutだけimbueを実行(wprintは出ないが、それ以外は表示される) -> これが一番実用的かも?

標準出力にユニコードを出力させる(wcoutだけimbueを実行)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <locale.h>
 
 
 
int _tmain
(
      int argc
    , _TCHAR* argv[]
)
{
    // ロケール変更
    std::wcout.imbue( std::locale( "", std::locale::ctype ) );
 
    // printf
    printf("printf  -> 日本語でた。");
    printf("¥n");
 
    // wprintf
    wprintf(L"wprintf -> 日本語でた。");
    printf("¥n");
 
    // cout
    std::cout << "cout    -> 日本語でた。";
    printf("¥n");
 
    // wcout
    std::wcout << L"wcout   -> 日本語でた。";
    printf("¥n");
 
 
 
    // 文字列
    std::wstring str = L"あいうえお¥n";
 
    // 標準出力へ出力する
    std::wcout << str << std::endl;
 
    // 正常終了
    return( 0 );
}

実行結果

printf  -> 日本語でた。
wprintf -> 
cout    -> 日本語でた。
wcout   -> 日本語でた。

setlocale(LC_CTYPE,"")と、wcoutのimbueを実行(coutは出ないが、それ以外は出る) -> coutが出ないのはちょっと問題あり

標準出力にユニコードを出力させる(setlocale(LC_CTYPE,"")と、wcoutのimbueを実行)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <locale.h>
 
 
 
int _tmain
(
      int argc
    , _TCHAR* argv[]
)
{
    // ロケール変更
    setlocale( LC_CTYPE, "" );
    std::wcout.imbue( std::locale( "", std::locale::ctype ) );
 
    // printf
    printf("printf  -> 日本語でた。");
    printf("¥n");
 
    // wprintf
    wprintf(L"wprintf -> 日本語でた。");
    printf("¥n");
 
    // cout
    std::cout << "cout    -> 日本語でた。";
    printf("¥n");
 
    // wcout
    std::wcout << L"wcout   -> 日本語でた。";
    printf("¥n");
 
    // 正常終了
    return( 0 );
}

実行結果

printf  -> 日本語でた。
wprintf -> 日本語でた。
cout    -> 
wcout   -> 日本語でた。

setlocale(LC_CTYPE,"")を実行(wprintは出るが、coutがでなくなる) -> cout, wcoutが出ないのはちょっと問題あり

標準出力にユニコードを出力させる(setlocale(LC_CTYPE,"")を実行)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <locale.h>
 
 
 
int _tmain
(
      int argc
    , _TCHAR* argv[]
)
{
    // ロケール変更
    setlocale( LC_CTYPE, "" );
 
    // printf
    printf("printf  -> 日本語でた。");
    printf("¥n");
 
    // wprintf
    wprintf(L"wprintf -> 日本語でた。");
    printf("¥n");
 
    // cout
    std::cout << "cout    -> 日本語でた。";
    printf("¥n");
 
    // wcout
    std::wcout << L"wcout   -> 日本語でた。";
    printf("¥n");
 
    // 正常終了
    return( 0 );
}

実行結果

printf  -> 日本語でた。
wprintf -> 日本語でた。
cout    -> 
wcout   -> 

何もしない

標準出力にユニコードを出力させる(何もしない)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <locale.h>
 
 
 
int _tmain
(
      int argc
    , _TCHAR* argv[]
)
{
    // printf
    printf("printf  -> 日本語でた。");
    printf("¥n");
 
    // wprintf
    wprintf(L"wprintf -> 日本語でた。");
    printf("¥n");
 
    // cout
    std::cout << "cout    -> 日本語でた。";
    printf("¥n");
 
    // wcout
    std::wcout << L"wcout   -> 日本語でた。";
    printf("¥n");
 
    // 正常終了
    return( 0 );
}

実行結果

printf  -> 日本語でた。
wprintf -> 
cout    -> 日本語でた。
wcout   -> 






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。