I'd like to ask you a question about Unicode Korean printout on C++.

Asked 2 years ago, Updated 2 years ago, 79 views

#include <iostream>
#include <clocale>
using namespace std;

int main() {
    setlocale(LC_ALL, "");

    wchar_t str1[] = L"WBCS 1";
    wchar_t str2[100];
    wchar_t str3[50];

    wcout << str1 << endl;
    wcout << L"string size: " << sizeof(str1) << endl;
    wcout << L"string length: " << wcslen(str1) << endl;

    wcout << endl;

    wcout << L"input string 1: ";
    wcin >> str2;
    wcout << L"input stirng 2: ";
    wcin >> str3;

    wcscat(str2, str3);

    wcout << L"string 1 + string 2: " << str2 << endl;

    return 0;
}

Although the encoding of the source file is set to UTF-8, it is not possible to input/output Korean in the wcin part.

What's the problem?

The compilation environment is version g++ 6.3.0.

c++ gcc

2022-09-21 15:14

2 Answers

(py36) allinux@allinux-DESKTOP:~:> g++ -o main2 main2.cpp
(py36) allinux@allinux-DESKTOP:~:> ./main2
WBCS 1
string size: 28
string length: 6

input string 1: Korean
input stirrng 2: kanadara
String 1 + string 2: Hangul
WBCS 1
string size: 14
string length: 6

input string 1: ABC
input stirrng 2: Korean
string 1 + string 2: Korean alphabet

I can print out Korean without any problems.

Linux's locale is the same as the answer above.


2022-09-21 15:14

We need to check the locale that is currently being applied by the locale command in the shell.

allinux@allinux-DESKTOP:~:> locale
LANG=ko_KR.UTF-8
LANGUAGE=
LC_CTYPE="ko_KR.UTF-8"
LC_NUMERIC="ko_KR.UTF-8"
LC_TIME="ko_KR.UTF-8"
LC_COLLATE="ko_KR.UTF-8"
LC_MONETARY="ko_KR.UTF-8"
LC_MESSAGES="ko_KR.UTF-8"
LC_PAPER="ko_KR.UTF-8"
LC_NAME="ko_KR.UTF-8"
LC_ADDRESS="ko_KR.UTF-8"
LC_TELEPHONE="ko_KR.UTF-8"
LC_MEASUREMENT="ko_KR.UTF-8"
LC_IDENTIFICATION="ko_KR.UTF-8"
LC_ALL=


2022-09-21 15:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.