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
,

locale을 적용하지 않은 경우:

#include <iostream>
#include <string>
#include <algorithm>
 
int main()
{
  std::string s;
  std::getline(std::cin, s);
  std::reverse(s.begin(), s.end()); // modifies s
  std::cout << s << std::endl;
  return 0;
}

 

컴파일:

$ g++ -o testReverseUTF8_003 testReverseUTF8_003.cpp

실행:

$ ./testReverseUTF8_003
안녕하세요?
?▒▒츄옕핅눕▒

 

locale을 적용하고 wstring, wcout, wcin을 사용한 경우:

#include <iostream>
#include <string>
#include <algorithm>
 
int main()
{
  std::setlocale(LC_ALL, "ko_KR.UTF-8");
  std::wstring s;
  std::getline(std::wcin, s);
  std::reverse(s.begin(), s.end()); // modifies s
  std::wcout << s << std::endl;
  return 0;
}

 

컴파일:

$ g++ -o testReverseUTF8_003 testReverseUTF8_003.cpp

실행:

$ ./testReverseUTF8_003
안녕하세요?
?요세하녕안


 

 

 

Posted by Scripter
,

다음은 utf-8 인코딩으로 저장한 C++ 소스이다.

Cygwin의 g++ tkdyd tl,  만일  아래의 소스에서
        std::setlocale(LC_ALL, "ko_KR.UTF-8");
대신  
        std::locale::global (std::locale ("ko_KR.UTF-8"));
로 하면 캄핑ㄹ은 되지만 실행 시에

        Segmentation fault (core dumped)

에러가 난다.

* 소스 파일명: testLocale_003.cpp

// Filename: testLocale_003.cpp
//
// Compile:  g++ -std=c++11 -o testLocale_003 testLocale_003.cpp
//    or         g++ -std=c++0x -o testLocale_003 testLocale_003.cpp
//    or         g++ -o testLocale_003 testLocale_003.cpp

#include <iostream>
#include <locale>
#include <cstring>   // for strlen()

using namespace std;

int main() {
  //// std::locale::global (std::locale ("ko_KR.UTF-8"));
  std::setlocale(LC_ALL, "en_US.UTF-8");
  wcout << L"한글\n";
  wcout << wcslen(L"한글") << L"\n";
  wcout << L"韓國語\n";
  wcout << wcslen(L"韓國語") << L"\n";

  cout << u8"한글\n";
  cout << strlen(u8"한글") << "\n";
  cout << u8"韓國語\n";
  cout << strlen(u8"韓國語") << "\n";
}

 

위의 소스에서 처럼 u8"스트링" 구문을 사용하면, g++ 명령으로 컴파일 시에 -std=c++11 또는 -std=gnu11 옵션을 주어야 한다.

 

* 컴파일

$ gcc -std=c++11 -o testLocale_003 testLocale_003.cpp

* 실행

$ ./testLocale_003

한글
2
韓國語
3
한글
6
韓國語
9

 

Posted by Scripter
,

우선 다음은 ms949 인코딩으로 저장한 C 소스이다.

* 소스 파일명: hello.c

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

int main()
{
    printf("Hello, 안녕하세요?\n");
    printf("strlen(\"Hello, 안녕하세요?\\n\") = %d\n", strlen("Hello, 안녕하세요?\n"));

 return 0;
}

* 컴파일

> gcc -o hello hello.c

*

 실행

> hello

Hello, 안녕하세요?
strlen("Hello, 안녕하세요?\n") = 19

 

다음은 utf-8 인코딩으로 저장한 C 소스이다.

* 소스 파일명: hello2.c

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

#define wstrlen wcslen

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

    printf("Using strlen():\n");
    printf("Hello, 안녕하세요?\n");
    printf("strlen(\"Hello, 안녕하세요?\\n\") = %d\n", strlen("Hello, 안녕하세요?\n"));

    wprintf(L"Using wcslen():\n");
 wprintf(L"Hello, 안녕하세요?\n");
    wprintf(L"wstrlen(\"Hello, 안녕하세요?\\n\") = %d\n", wstrlen(L"Hello, 안녕하세요?\n"));

 return 0;
}

* 컴파일

> gcc -o hello2 hello2.c

* 실행

> hello2

Using strlen():
Hello, ?덈뀞?섏꽭??
strlen("Hello, ?덈뀞?섏꽭??\n") = 24
Using wcslen():
Hello, 안녕하세요?
wstrlen("Hello, 안녕하세요?\n") = 14

 

다음도 utf-8 인코딩으로 저장한 C 소스이다.

* 소스 파일명: hello3.c

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

#define wstrlen wcslen

int main()
{
 char as[] = "Hello, 안녕하세요?";
 wchar_t bs[] = L"Hello, 안녕하세요?";

 setlocale(LC_ALL,"");

    printf("Using strlen():\n");
    printf("Hello, 안녕하세요?\n");
    printf("strlen(\"Hello, 안녕하세요?\\n\") = %d\n", strlen("Hello, 안녕하세요?\n"));
    printf("strlen(\"%s\") = %d\n", as, strlen(as));
    printf("\n");

    wprintf(L"Using wcslen():\n");
 wprintf(L"Hello, 안녕하세요?\n");
    wprintf(L"wstrlen(\"Hello, 안녕하세요?\\n\") = %d\n", wstrlen(L"Hello, 안녕하세요?\n"));
    wprintf(L"wstrlen(\"%s\") = %d\n", bs, wstrlen(bs));

 return 0;
}

* 컴파일

> gcc -o hello3 hello3.c

* 실행

> hello3

Using strlen():
Hello, ?덈뀞?섏꽭??
strlen("Hello, ?덈뀞?섏꽭??\n") = 24
strlen("Hello, ?덈뀞?섏꽭??") = 23

Using wcslen():
Hello, 안녕하세요?
wstrlen("Hello, 안녕하세요?\n") = 14
wstrlen("Hello, 안녕하세요?") = 13

 

 

Posted by Scripter
,