wcout, wprintf() only print English

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ioannis Vranos

    wcout, wprintf() only print English

    Has anyone actually managed to print non-English text by using wcout or
    wprintf and the rest of standard, wide character functions?
  • Ioannis Vranos

    #2
    Re: wcout, wprintf() only print English

    Ioannis Vranos wrote:
    Has anyone actually managed to print non-English text by using wcout or
    wprintf and the rest of standard, wide character functions?

    For example:

    [john@localhost src]$ cat main.cc
    #include <iostream>

    int main()
    {
    using namespace std;

    wcout<< L"Äïêéìáóôéê ü ìÞíõìá\n";
    }

    [john@localhost src]$ ./foobar-cpp
    ??????????? ??????

    [john@localhost src]$

    Comment

    • Rolf Magnus

      #3
      Re: wcout, wprintf() only print English

      Ioannis Vranos wrote:
      Ioannis Vranos wrote:
      >Has anyone actually managed to print non-English text by using wcout or
      >wprintf and the rest of standard, wide character functions?
      >
      >
      For example:
      >
      [john@localhost src]$ cat main.cc
      #include <iostream>
      >
      int main()
      {
      using namespace std;
      >
      wcout<< L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό μήνυμα\n" ;
      Are you sure that you stored your source file in the same encoding the
      compiler expects as source character set?
      }
      >
      [john@localhost src]$ ./foobar-cpp
      ??????????? ??????
      >
      [john@localhost src]$

      Comment

      • Ioannis Vranos

        #4
        Re: wcout, wprintf() only print English

        Rolf Magnus wrote:
        Ioannis Vranos wrote:
        >
        >Ioannis Vranos wrote:
        >>Has anyone actually managed to print non-English text by using wcout or
        >>wprintf and the rest of standard, wide character functions?
        >>
        >For example:
        >>
        >[john@localhost src]$ cat main.cc
        >#include <iostream>
        >>
        >int main()
        >{
        > using namespace std;
        >>
        > wcout<< L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό μήνυμα\n" ;
        >
        Are you sure that you stored your source file in the same encoding the
        compiler expects as source character set?
        >
        >}
        >>
        >[john@localhost src]$ ./foobar-cpp
        >??????????? ??????
        >>
        >[john@localhost src]$

        Well I created the file with anjuta editor with the message being a
        Greek one. The Greek message also appears the same when I display the
        source file in the console.

        I suppose it is saved as UTF8.


        Also the code

        #include <iostream>
        #include <string>

        int main()
        {
        using namespace std;

        wstring s;

        wcin>s;


        wcout<< s<< endl;
        }


        displays nothing when I enter greek text.


        Should I mess with locales?

        Comment

        • Ioannis Vranos

          #5
          Re: wcout, wprintf() only print English

          Ioannis Vranos wrote:
          Rolf Magnus wrote:
          >Ioannis Vranos wrote:
          >>
          >>Ioannis Vranos wrote:
          >>>Has anyone actually managed to print non-English text by using wcout or
          >>>wprintf and the rest of standard, wide character functions?
          >>>
          >>For example:
          >>>
          >>[john@localhost src]$ cat main.cc
          >>#include <iostream>
          >>>
          >>int main()
          >>{
          >> using namespace std;
          >>>
          >> wcout<< L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό μήνυμα\n" ;
          >>
          >Are you sure that you stored your source file in the same encoding the
          >compiler expects as source character set?
          >>
          >>}
          >>>
          >>[john@localhost src]$ ./foobar-cpp
          >>??????????? ??????
          >>>
          >>[john@localhost src]$
          >
          >
          Well I created the file with anjuta editor with the message being a
          Greek one. The Greek message also appears the same when I display the
          source file in the console.
          >
          I suppose it is saved as UTF8.
          >
          >
          Also the code
          >
          #include <iostream>
          #include <string>
          >
          int main()
          {
          using namespace std;
          >
          wstring s;
          >
          wcin>s;
          >
          >
          wcout<< s<< endl;
          }
          >
          >
          displays nothing when I enter greek text.

          both in g++ under Linux and VC++ 2008 Express under Windows, with the
          latest saving the source code file as Unicode after it detected
          non-english text.

          Should I mess with locales?

          Comment

          • Ioannis Vranos

            #6
            Re: wcout, wprintf() only print English

            Made more precise:

            Ioannis Vranos wrote:
            >>>For example:
            >>>>
            >>>[john@localhost src]$ cat main.cc
            >>>#include <iostream>
            >>>>
            >>>int main()
            >>>{
            >>> using namespace std;
            >>>>
            >>> wcout<< L"Äïêéìáóôéê ü ìÞíõìá\n";
            >>>
            >>Are you sure that you stored your source file in the same encoding the
            >>compiler expects as source character set?
            >>>
            >>>}
            >>>>
            >>>[john@localhost src]$ ./foobar-cpp
            >>>?????????? ? ??????
            >>>>
            >>>[john@localhost src]$
            >>
            >>
            >Well I created the file with anjuta editor with the message being a
            >Greek one. The Greek message also appears the same when I display the
            >source file in the console.
            >>
            >I suppose it is saved as UTF8.
            >>
            >>
            >Also the code
            >>
            >#include <iostream>
            >#include <string>
            >>
            >int main()
            >{
            > using namespace std;
            >>
            > wstring s;
            > wcin>s;
            >>
            > wcout<< s<< endl;
            >}
            >>
            >>
            displays the Greek text when I enter it, but outputs nothing. With
            English text, the text is displayed both when entered and outputed.


            [john@localhost src]$ ./foobar-cpp
            Äïêéìáóôéêü

            [john@localhost src]$ ./foobar-cpp
            Test
            Test
            [john@localhost src]$




            both in g++ under Linux and VC++ 2008 Express under Windows, with the
            latest saving the source code file as Unicode after it detected
            non-english text.
            >
            >
            >Should I mess with locales?

            Comment

            • Jeff Schwab

              #7
              Re: wcout, wprintf() only print English

              Ioannis Vranos wrote:
              Ioannis Vranos wrote:
              >Has anyone actually managed to print non-English text by using wcout
              >or wprintf and the rest of standard, wide character functions?
              >
              >
              For example:
              >
              [john@localhost src]$ cat main.cc
              #include <iostream>
              >
              int main()
              {
              using namespace std;
              >
              wcout<< L"Äïêéìáóôéê ü ìÞíõìá\n";
              }
              >
              [john@localhost src]$ ./foobar-cpp
              ??????????? ??????
              >
              [john@localhost src]$
              Hmmm... I work almost entirely in English, so this error message is new
              to me:

              $ make
              g++ -ansi -pedantic -Wall main.cc -o main
              main.cc: In function 'int main()':
              main.cc:4: error: converting to execution character set: Invalid or
              incomplete multibyte or wide character
              make: *** [main] Error 1

              Comment

              • Boris

                #8
                Re: wcout, wprintf() only print English

                On Sat, 23 Feb 2008 13:11:09 +0200, Ioannis Vranos
                <ivranos@nospam .no.spamfreemai l.grwrote:
                [...]
                >>Also the code
                [...]displays the Greek text when I enter it, but outputs nothing. With
                English text, the text is displayed both when entered and outputed.
                I don't remember anymore the details but the problem has something to do
                with codecvt: Your wide characters are automatically converted to narrow
                characters by wcout. This is something you might not want (and even if you
                want it the conversion might not work automatically the way you expect :).

                Try writing to wstringstream and converting to UTF-8 explicitly (storing
                the result eg. in string). If your console supports UTF-8 you can print to
                cout (otherwise print to a file so you can test the output in an editor).

                HTH,
                Boris

                Comment

                • Ioannis Vranos

                  #9
                  Re: wcout, wprintf() only print English

                  Jeff Schwab wrote:
                  Ioannis Vranos wrote:
                  >Ioannis Vranos wrote:
                  >>Has anyone actually managed to print non-English text by using wcout
                  >>or wprintf and the rest of standard, wide character functions?
                  >>
                  >>
                  >For example:
                  >>
                  >[john@localhost src]$ cat main.cc
                  >#include <iostream>
                  >>
                  >int main()
                  >{
                  > using namespace std;
                  >>
                  > wcout<< L"Äïêéìáà ³Ã´Ã©ÃªÃ¼ ìÞíõìá\n" ;
                  >}
                  >>
                  >[john@localhost src]$ ./foobar-cpp
                  >??????????? ??????
                  >>
                  >[john@localhost src]$
                  >
                  Hmmm... I work almost entirely in English, so this error message is new
                  to me:
                  >
                  $ make
                  g++ -ansi -pedantic -Wall main.cc -o main
                  main.cc: In function 'int main()':
                  main.cc:4: error: converting to execution character set: Invalid or
                  incomplete multibyte or wide character
                  make: *** [main] Error 1

                  I tried the same:

                  [john@localhost src]$ g++ -ansi -pedantic-errors -Wall main.cc -o
                  foobar-cpp

                  [john@localhost src]$


                  Perhaps when you copy and paste the greek text, you copy garbage (that
                  is, not viewing the message in the correct character set in your
                  newsgroup reader).


                  So, I repost the code in this message which is encoded to Unicode (UTF-8):


                  #include <iostream>

                  int main()
                  {
                  using namespace std;

                  wcout<< L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό μήνυμα\n" ;
                  }

                  Comment

                  • Jeff Schwab

                    #10
                    Re: wcout, wprintf() only print English

                    Ioannis Vranos wrote:
                    Jeff Schwab wrote:
                    >Ioannis Vranos wrote:
                    >>Ioannis Vranos wrote:
                    >>>Has anyone actually managed to print non-English text by using wcout
                    >>>or wprintf and the rest of standard, wide character functions?
                    >>>
                    >>>
                    >>For example:
                    >>>
                    >>[john@localhost src]$ cat main.cc
                    >>#include <iostream>
                    >>>
                    >>int main()
                    >>{
                    >> using namespace std;
                    >>>
                    >> wcout<< L"Äïêéìáà ³Ã´Ã©ÃªÃ¼ ìÞíõìá\n" ;
                    >>}
                    >>>
                    >>[john@localhost src]$ ./foobar-cpp
                    >>??????????? ??????
                    >>>
                    >>[john@localhost src]$
                    >>
                    >Hmmm... I work almost entirely in English, so this error message is
                    >new to me:
                    >>
                    >$ make
                    >g++ -ansi -pedantic -Wall main.cc -o main
                    >main.cc: In function 'int main()':
                    >main.cc:4: error: converting to execution character set: Invalid or
                    >incomplete multibyte or wide character
                    >make: *** [main] Error 1
                    >
                    >
                    I tried the same:
                    >
                    [john@localhost src]$ g++ -ansi -pedantic-errors -Wall main.cc -o
                    foobar-cpp
                    >
                    [john@localhost src]$
                    >
                    >
                    Perhaps when you copy and paste the greek text, you copy garbage (that
                    is, not viewing the message in the correct character set in your
                    newsgroup reader).
                    >
                    >
                    So, I repost the code in this message which is encoded to Unicode (UTF-8):
                    >
                    >
                    #include <iostream>
                    >
                    int main()
                    {
                    using namespace std;
                    >
                    wcout<< L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό μήνυμα\n" ;
                    }
                    Thanks, you were correct.

                    Here's what I thought was "supposed" to be the portable solution:

                    #include <iostream>
                    #include <locale>

                    int main() {
                    std::wcout.imbu e(std::locale(" el_GR.UTF-8"));
                    std::wcout << L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό μήνυμα\n" ;
                    }

                    However, my system still shows question marks for this. For whatever
                    it's worth, here's the (probably incorrect) way that appears to work on
                    my system:

                    #include <iostream>
                    #include <locale>

                    int main() {
                    std::cout.imbue (std::locale("" ));
                    std::cout << "Δοκιμασ τικό μήνυμα\n" ;
                    }

                    Comment

                    • Ioannis Vranos

                      #11
                      Re: wcout, wprintf() only print English

                      Based on the MSDN example:


                      // basic_ios_imbue .cpp
                      // compile with: /EHsc
                      #include <iostream>
                      #include <locale>

                      int main( )
                      {
                      using namespace std;

                      cout.imbue( locale( "french_fra nce" ) );
                      double x = 1234567.123456;
                      cout << x << endl;
                      }


                      that doesn't work in my GCC, this works:

                      #include <iostream>
                      #include <limits>

                      int main()
                      {
                      using namespace std;

                      cout.imbue( locale( "greek" ) );

                      cout<< "Δοκιμασ τικό\n";
                      }


                      This also works:

                      #include <iostream>
                      #include <limits>

                      int main()
                      {
                      using namespace std;

                      cout.imbue( locale( "en_US" ) );

                      cout<< "Δοκιμασ τικό\n";
                      }




                      Crazy stuff.

                      Comment

                      • Ioannis Vranos

                        #12
                        Re: wcout, wprintf() only print English

                        It looks like GCC has the opposite stuff, cout, cin, string work as
                        wcout, wcin, wstring and vice versa! Bug?



                        #include <iostream>

                        int main()
                        {
                        using namespace std;

                        wstring ws;

                        wcin>ws;

                        cout<< ws.size()<< endl;
                        }



                        [john@localhost src]$ ./foobar-cpp
                        Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό
                        0
                        [john@localhost src]$



                        #include <iostream>

                        int main()
                        {
                        using namespace std;

                        string s;

                        cin>s;

                        cout<< s.size()<< endl;
                        }


                        [john@localhost src]$ ./foobar-cpp
                        Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό
                        22
                        [john@localhost src]$


                        #include <iostream>

                        int main()
                        {
                        using namespace std;

                        string s;

                        cin>s;

                        cout<< s<< endl;
                        }


                        [john@localhost src]$ ./foobar-cpp
                        Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό
                        Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό
                        [john@localhost src]$



                        #include <iostream>

                        int main()
                        {
                        using namespace std;

                        wstring ws;

                        wcin>ws;

                        wcout<< ws<< endl;
                        }


                        [john@localhost src]$ ./foobar-cpp
                        Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό

                        [john@localhost src]$



                        #include <iostream>

                        int main()
                        {
                        using namespace std;

                        cout<< "Δοκιμασ τικό-11\n";

                        wcout<< "Δοκιμασ τικό-22\n";

                        cout<< L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό-33\n";

                        wcout<< L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό-44\n";
                        }


                        [john@localhost src]$ ./foobar-cpp
                        Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό-11
                        -22
                        0x80488c8�� ����� ��-44
                        [john@localhost src]$



                        Conclusion: It appears GCC has the wide character stuff messed up, or I
                        am missing important knowledge.

                        Comment

                        • Jeff Schwab

                          #13
                          Re: wcout, wprintf() only print English

                          Ioannis Vranos wrote:
                          It looks like GCC has the opposite stuff, cout, cin, string work as
                          wcout, wcin, wstring and vice versa! Bug?
                          ....
                          Conclusion: It appears GCC has the wide character stuff messed up, or I
                          am missing important knowledge.
                          You and me both. I would be very surprised if this were a GCC bug (I'm
                          using 4.2.4 pre-release), but I'm guessing somebody here knows a lot
                          more about this than we do, and is willing to enlighten us. :)

                          Comment

                          • Ioannis Vranos

                            #14
                            Re: wcout, wprintf() only print English

                            I posted the following to c.l.c., and I think it is useful to post it
                            here too:


                            [The current message encoding is set to Unicode (UTF-8) because it
                            contains Greek]


                            The following code does not work as expected:


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

                            int main()
                            {
                            char *p= setlocale( LC_ALL, "Greek" );

                            wchar_t input[50];

                            if (!p)
                            printf("NULL returned!\n");

                            fgetws(input, 50, stdin);

                            wprintf(L"%s\n" , input);

                            return 0;
                            }


                            Under Linux:


                            [john@localhost src]$ ./foobar-cpp
                            Test
                            T
                            [john@localhost src]$


                            [john@localhost src]$ ./foobar-cpp
                            Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό
                            �
                            [john@localhost src]$




                            Under MS Visual C++ 2008 Express:

                            Test
                            Test

                            Press any key to continue . . .


                            Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό
                            ??????ε????

                            Press any key to continue . . .


                            Am I missing something?

                            Comment

                            • Ioannis Vranos

                              #15
                              Re: wcout, wprintf() only print English

                              Reply I posted in c.l.c.:


                              Ioannis Vranos wrote:
                              >
                              It works OK under Terminal UTF-8 default encoding too. So "%ls" is what
                              was really needed.

                              Actually the code:

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

                              int main()
                              {
                              char *p= setlocale( LC_ALL, "Greek" );

                              wprintf(L"ΔοΠºÎ¹Î¼Î±ÏƒÏ„ικ ÏŒ\n");

                              return 0;
                              }

                              works only when I set the Terminal encoding to Greek (ISO-8859-7).


                              >
                              >
                              BTW, how can we define UTF-8 as the locale?
                              >
                              >
                              Thanks a lot.

                              Comment

                              Working...