Web lists-archives.org

Re: [Mingw-users] Problem with setlocale




2008/4/16 Немос <nemos-linux@xxxxxxxxx>:
> I have some problem with setlocale() - when I try to change
>  output code page it does not work. I use this example to
>  test setlocale():
>
>  #include <stdio.h>
>  #include <locale.h>
>
>  int main(int argc, char *argv[])
>  {
>     try
>        {
>           char  text[]="Привет";
>           setlocale(LC_CTYPE,".866");
>           puts(text);
>           setlocale(LC_CTYPE,".1251");
>           puts(text);
>        }
>     catch (...)
>        {
>           printf("catch exception");
>        };
>     return 0;
>  };
>
>  Build by command
>  g++ main.cpp -o 1.exe
>
>  Program print two identical string, but in different code
>  page it must be different text.
>
>  I test it with gcc-*-3.4.5-20060117-1 and
>  gcc-*-3.4.5-20060117-2.
>
>  In VS this code work fine.

I do not understand why you said "In VS this code work fine". On the
contrary, my test with Visual Studio 2003/2005 shows that the output
is exactly the same.

I know one way that will output different strings in VS. If you use
wchar_t and _putws, things will be different, since _putws will
convert the encoding from UTF-16 to the console code page. You should
save the source file in UTF-16, in this case; otherwise the result is
unreliable, since you cannot tell MSVC the source encoding, and the
system legacy encoding is always used, which may or may not be what
you want.

AFAIK, MinGW will always interpret the input literals as Latin-1, so
it does not work in MinGW. I think you will need to use the \u
sequences in this case. The following code works in both MSVC 7.1/8.0
and MinGW, and probably achieves what you want.

  wchar_t  text[]=L"\u041f\u0440\u0438\u0432\u0435\u0442";
  setlocale(LC_CTYPE,".866");
  _putws(text);
  setlocale(LC_CTYPE,".1251");
  _putws(text);

Best regards,

Yongwei

-- 
Wu Yongwei
URL: http://wyw.dcweb.cn/
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users