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 <iostream>
// #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 가나다

 

 

 

 

Posted by Scripter
,

Cygwin 에서는 printf 와 wprint 를 동시에 사용하는 경우, 컴파일은 성공하지만 실행하면 wprintf 가 제대로 동작하지 않는다.  그리고 wprint 나 swptinf 사용 시 스트링을 출력하기 위해서는 %s 대신 %ls 포맷을 사용해야 한다.

Cygwin 의 경우 wchar_t 의 크기는 2바이트이다.

 

#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>

int main(int argc, const char *argv[]) {
    wchar_t wt[100];

    /// 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");

    wprintf(L"sizeof(wchar_t) = %d\n", sizeof(wchar_t));
    wprintf(L"For more informations, \nsee: http://www.firstobject.com/wchar_t-string-on-linux-osx-windows.htm\n");

    wprintf(L"\n");
    wcscpy(wt, L"abc 가나다");
    wprintf(L"%ls\n", wt);
    swprintf(wt, wcslen(L"abc 가나다") + 1, L"%ls", L"abc 가나다");
    wprintf(L"%ls\n", wt);
    wprintf(L"%ls\n", L"abc 가나다");
    wprintf(L"abc 가나다\n");

    return 0;
}

 

컴파일:

$ gcc -o testWcharT_02 testWcharT_02.c

실행:

$ ./testWchaTr_02

sizeof(wchar_t) = 2
For more informations,
see: http://www.firstobject.com/wchar_t-string-on-linux-osx-windows.htm

abc 가나다
abc 가나다
abc 가나다
abc 가나다

 

 

 

 

Posted by Scripter
,