Cygwin 에서는 printf 와 wprint 를 동시에 사용하는 경우, 컴파일은 성공하지만 실행하면 wprintf 가 제대로 동작하지 않는다. 그리고 wprint 나 swptinf 사용 시 스트링을 출력하기 위해서는 %s 대신 %ls 포맷을 사용해야 한다.
Cygwin 의 경우 wchar_t 의 크기는 2바이트이다.
C++ 언어에서는 스트링(문자열) 클래스인 string 이 준비되어 있지만, utf-8 스트링을 db위해서는 wstring 을 써야 한다. 또 표준 입출력 cin 과 cout 대신 wcin 과 wcout 을 써야 허며 이를 위해서는 헤더 파일 iostream 을 인클루드(포함)해야 하고 이름 공간 std 를 써야 한다. 또 C 언어 처럼 setlocale(LC_ALL,""); 도 해야 한다. 헤더 파일 locale.h 는 포함하지 않아도 된다.
http://blog.naver.com/PostView.nhn?blogId=manhwamani&logNo=10083589211 도 참조한다.
// #include <cwchar>
// #include <string>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
// #include <locale.h>
int main(int argc, const char *argv[]) {
wchar_t wt[100];
std::wstring ws;
/// setlocale(LC_ALL,"kr_KR.UTF-8");
setlocale(LC_ALL,"");
// printf("sizeof(wchar_t) = %d\n", sizeof(wchar_t));
// printf("For more informations, \nsee: http://www.firstobject.com/wchar_t-string-on-linux-osx-windows.htm\n");
std::wcout << L"sizeof(wchar_t) = " << sizeof(wchar_t) << std::endl;
std::wcout << L"For more informations, \nsee: http://www.firstobject.com/wchar_t-string-on-linux-osx-windows.htm" << std::endl;
std::wcout << L"\n";
wcscpy(wt, L"abc 가나다");
std::wcout << L"Using wchar_t[],\n";
std::wcout << wt << std::endl;
swprintf(wt, wcslen(L"abc 가나다") + 1, L"%ls", L"abc 가나다");
std::wcout << wt << std::endl;
std::wcout << L"The length of \"" << wt << L"\" is " << wcslen(wt) << L"." << std::endl;
wprintf(L"%ls\n", L"abc 가나다");
std::wcout << L"\n";
ws = L"abc 가나다";
std::wcout << L"Using wstring,\n";
std::wcout << ws << std::endl;
std::wcout << L"The length of \"" << ws << L"\" is " << ws.length() << L"." << std::endl;
std::wcout << L"abc 가나다" << std::endl;
return 0;
}
컴파일:
$ g++ -o testWstring_02 testWstring_02.cpp
실행:
$ ./testWstring_02
sizeof(wchar_t) = 2
For more informations,
see: http://www.firstobject.com/wchar_t-string-on-linux-osx-windows.htm
Using wchar_t[],
abc 가나다
abc 가나다
The length of "abc 가나다" is 7.
abc 가나다
Using wstring,
abc 가나다
The length of "abc 가나다" is 7.
abc 가나다
'프로그래밍 > C++' 카테고리의 다른 글
utf8 인코딩을 이용한 다국어 hello world (0) | 2021.01.23 |
---|---|
C++ 에서 C 함수를 불러 사용하기 (0) | 2021.01.07 |
cygwin 의 g++ 로 utf-8 문자열 거꾸로하기 (0) | 2014.04.10 |
cygwin/mingw 의 g++ 로 utf-8 한글 처리하기 (0) | 2014.04.10 |
Qt 5.2.1 의 Qt Creater 3.0.1 을 이용한 Hello 예제 작성하기 (0) | 2014.02.28 |