user3924427
user3924427

Reputation: 53

How to print Japanese character in windows Japanese system?

I have written a small program that will take Japanese string as the hardcoded values and will write it into the file in the Japanese system. The program is written in visual studio 2010(english system)

char szMsg_m[]="スプールがいっぱいです";  //input
string szMsg_s=("スプールがいっぱいです");
wchar_t szMsg_w[]=L"スプールがいっぱいです";
wstring szMsg_h=L"スプールがいっぱいです";

fprintf (trace.TraceFP, "\tMsg:%s \n", szMsg_m); //output
fprintf (trace.TraceFP, "\tMsg:%s \n", szMsg_s.c_str());
fwprintf (trace.TraceFP, L"\tMsg:%s \n", szMsg_w);
fwprintf (trace.TraceFP, L"\tMsg:%s \n", szMsg_h.c_str());

The output should be Japanese why it is coming as (?) when I am running the exe in Japanese system

Upvotes: 2

Views: 526

Answers (1)

Duck Dodgers
Duck Dodgers

Reputation: 3461

As user694733 pointed out, you are missing the std::locale::global (std::locale ("en_US.UTF-8"));. I tested the following:

#include <iostream>
#include <string>
#include <cstdio>

int main()
{
    std::locale::global (std::locale ("en_US.UTF-8"));
    char szMsg_m[]="スプールがいっぱいです";  //input
    std::string szMsg_s=("スプールがいっぱいです");

    fprintf (stdout, "\tMsg:%s \n", szMsg_m); //output
    fprintf (stdout, "\tMsg:%s \n", szMsg_s.c_str());    
    return 0;
}

which produces the output:

Msg:スプールがいっぱいです

Msg:スプールがいっぱいです

What I cannot tell, without more information is, what trace.TraceFP in your code is, and how to use it.

What also works is the following:

{
    std::locale::global (std::locale ("en_US.UTF-8"));
    wchar_t szMsg_w[]=L"スプールがいっぱいです";
    std::wstring szMsg_h=L"スプールがいっぱいです";
    fwprintf (stdout, L"\tMsg:%ls \n", szMsg_w);
    fwprintf (stdout, L"\tMsg:%ls \n", szMsg_h.c_str());
}

Notice that the correct format for the wide-strings is %ls.

which produces the output:

Msg:スプールがいっぱいです

Msg:スプールがいっぱいです

Upvotes: 2

Related Questions