473,785 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Feb 23 '08
44 4386
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.
Feb 23 '08 #11
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.

Feb 23 '08 #12
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. :)
Feb 23 '08 #13
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?
Feb 23 '08 #14
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.
Feb 23 '08 #15
How can we convert the C subset C++ code:
#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"%ls", input);

return 0;
}

that works, to use the newest and greatest C++ facilities? :-)
Feb 23 '08 #16
James Kanze wrote:
>
>Also I have MS Visual C++ 2008 Express installed.

Under Linux ! :-)

Linux::VMWare:: Windows::VC++20 08 Express.
Feb 24 '08 #17
Ioannis Vranos wrote:
How can we convert the C subset C++ code:
#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"%ls", input);

return 0;
}

that works, to use the newest and greatest C++ facilities? :-)

The next best thing after this, is to use the C-subset setlocale with
wcin, wcout, wstring and stuff, and it works indeed:
#include <iostream>
#include <clocale>
#include <string>

int main()
{
using namespace std;

char *p= setlocale( LC_ALL, "greek" );

if (!p)
cerr<< "NULL returned!\n";

wstring ws;

wcin>ws;

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

The following works out of the box too:

#include <iostream>
#include <clocale>

int main()
{
using namespace std;

char *p= setlocale( LC_ALL, "greek" );

wcout<< L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό\n";
}
[john@localhost src]$ ./foobar-cpp
Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό
[john@localhost src]$

Now how can we move from the setlocale() to the newer C++ facilities?
Feb 24 '08 #18
On Feb 24, 1:00 am, Ioannis Vranos <ivra...@nospam .no.spamfreemai l.gr>
wrote:
James Kanze wrote:
Also I have MS Visual C++ 2008 Express installed.
Under Linux ! :-)
Linux::VMWare:: Windows::VC++20 08 Express.
Thanks. I'll give it a try myself. (Of course, the executables
it generates will also require VMWare to run, but it will allow
at least verifying that my code compiles with VC++ before trying
to port it to Windows.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 24 '08 #19
On Sat, 23 Feb 2008 23:19:57 +0200, Ioannis Vranos
<iv*****@nospam .no.spamfreemai l.grwrote:
Alf P. Steinbach wrote:
[...]
>Ans as has also been remarked else-thread, by Boris, one issue,
relevant for i/o, is that the wide character streams convert to and
from narrow characters. wcout converts to narrow characters, and wcin
converts from narrow characters. They're not wide character streams,
they're wide character converters.

I am not sure I understand this.

Isn't L"some text" a wide character string literal? Don't wcout, wcin
and wstring provide operator<< and operator>overlo ads for wide
characters and wide character strings?
wcout and wcin represent external devices. When you read from or write to
external devices the facet codecvt is used. The C++ standard says there
are only two: codecvt<char, char, mbstate_twhich doesn't do anything and
codecvt<wchar_t , char, mbstate_twhich converts from wchar_t to char. As
you see there is an implicit conversion to char even if you actually use
wchar_t in your program. You don't know either how the conversion of
codecvt<wchar_t , char, mbstate_tworks (there is no guarantee that it's
UTF-16 to UTF-8 for example). Either you convert to UTF-8 explicitly and
write to cout or you define or use a codecvt from a third-party library
(like http://www.boost.org/libs/serializat.../codecvt.html).

Boris
[...]
Feb 24 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.