473,785 Members | 2,744 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
Boris wrote:
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).

Instead of messing with these details, perhaps we should accept that the
C subset setlocale() function defined in <clocaleis simpler (and thus
better)?
The following code works:
#include <iostream>
#include <clocale>
#include <string>
#include <cstdlib>
int main()
{
using namespace std;

if (!setlocale( LC_ALL, "greek" ))
{
cerr<< "NULL returned!\n";

return EXIT_FAILURE;
}
wstring ws;

wcin>ws;

wcout<< ws<< endl;
}
Feb 24 '08 #21
I filed a bug in GCC Bugzilla:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35353
May anyone explain me how should I apply the suggested solution
"sync_with_stdi o (false)", and the fstream suggested solution to the
following failing code?

#include <iostream>
#include <locale>
#include <string>

int main()
{
using namespace std;

wcout.imbue(loc ale("greek"));

wstring ws;

wcin>ws;

wcout<< ws<< endl;
}

Feb 24 '08 #22
On Sun, 24 Feb 2008 16:46:26 +0200, Ioannis Vranos
<iv*****@nospam .no.spamfreemai l.grwrote:
[...]Instead of messing with these details, perhaps we should accept
that the C subset setlocale() function defined in <clocaleis simpler
(and thus better)?
The following code works:
#include <iostream>
#include <clocale>
#include <string>
#include <cstdlib>
int main()
{
using namespace std;

if (!setlocale( LC_ALL, "greek" ))
{
cerr<< "NULL returned!\n";

return EXIT_FAILURE;
}
wstring ws;

wcin>ws;

wcout<< ws<< endl;
}
If the locale name "greek" means an eight-bit character set is used you
don't need to use wstring, wcin and wcout at all? What character set do
you actually plan to use in your program?

Boris
Feb 24 '08 #23
Boris wrote:
On Sun, 24 Feb 2008 16:46:26 +0200, Ioannis Vranos
<iv*****@nospam .no.spamfreemai l.grwrote:
>[...]Instead of messing with these details, perhaps we should accept
that the C subset setlocale() function defined in <clocaleis simpler
(and thus better)?
The following code works:
#include <iostream>
#include <clocale>
#include <string>
#include <cstdlib>
int main()
{
using namespace std;

if (!setlocale( LC_ALL, "greek" ))
{
cerr<< "NULL returned!\n";

return EXIT_FAILURE;
}
wstring ws;

wcin>ws;

wcout<< ws<< endl;
}

If the locale name "greek" means an eight-bit character set is used you
don't need to use wstring, wcin and wcout at all? What character set do
you actually plan to use in your program?

Boris

I am only experimenting with the wide character support. Is there any
possibility the "greek" locale to be only a 8-bit one? And if it is, how
can it also display english (latin characters)? By using something like
ASCII extended (0-127 latin characters and 128-255 greek characters)?
"locale -a" also displays the following greek encodings:
el_GR
el_GR.iso88597
el_GR.utf8

The following also works:
#include <iostream>
#include <clocale>
#include <string>
#include <cstdlib>

int main()
{
using namespace std;

if (!setlocale( LC_ALL, "el_GR.utf8 " ))
{
cerr<< "NULL returned!\n";

return EXIT_FAILURE;
}

wstring ws;

wcin>ws;

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

#include <iostream>
#include <locale>
#include <string>
#include <cstdlib>
int main()
{
using namespace std;

wcout.imbue(loc ale("el_GR.utf8 "));

wstring ws;

wcin>ws;

wcout<< ws<< endl;
}

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

[john@localhost src]$
Feb 24 '08 #24
I managed to make it work under the newer C++ facilities, after having a
look in ISO C++ example code:
The following codes work:
#include <iostream>
#include <locale>
#include <string>
int main()
{
using namespace std;

locale::global( locale("greek") );

wstring ws;

wcin>ws;

wcout<< ws<< endl;
}

[john@localhost src]$ ./foobar-cpp
Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό
Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό
[john@localhost src]$
along with el_GR, el_GR.iso88597, el_GR.utf8. For example the following
code also works:
#include <iostream>
#include <locale>
#include <string>
int main()
{
using namespace std;

locale::global( locale("el_GR.u tf8"));

wstring ws;

wcin>ws;

wcout<< ws<< endl;
}
[john@localhost src]$ ./foobar-cpp
Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό
Î”Î¿ÎºÎ¹Î¼Î±ÏƒÏ „ικό
[john@localhost src]$
Feb 24 '08 #25
Ioannis Vranos wrote:
I filed a bug in GCC Bugzilla:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35353
Ahh, sanity from the Gnu. :)

May anyone explain me how should I apply the suggested solution
"sync_with_stdi o (false)", and the fstream suggested solution to the
following failing code?
fstream has nothing to do with it. The Gnu developer just thought you
might have used sync_with_stdio before with file streams. What he means
is that before you do any I/O, you should make a call like this:

std::ios_base:: sync_with_stdio (false);

The following program, as far as I can tell, works find on my system.

#include <iostream>
#include <locale>

int main()
{
std::ios_base:: sync_with_stdio (false);

std::wcout.imbu e(std::locale(" el_GR.UTF-8"));

std::wcout<< L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό μήνυμα\n" ;
std::wcout.flus h();
}
Feb 24 '08 #26
Jeff Schwab wrote:
Ioannis Vranos wrote:
>I filed a bug in GCC Bugzilla:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35353

Ahh, sanity from the Gnu. :)

>May anyone explain me how should I apply the suggested solution
"sync_with_std io (false)", and the fstream suggested solution to the
following failing code?

fstream has nothing to do with it. The Gnu developer just thought you
might have used sync_with_stdio before with file streams. What he means
is that before you do any I/O, you should make a call like this:

std::ios_base:: sync_with_stdio (false);

The following program, as far as I can tell, works find on my system.

#include <iostream>
#include <locale>

int main()
{
std::ios_base:: sync_with_stdio (false);

std::wcout.imbu e(std::locale(" el_GR.UTF-8"));

std::wcout<< L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό μήνυμα\n" ;
std::wcout.flus h();
}

Yes it works OK here too. How does desynchronizing with stdio makes the
program work?
Feb 24 '08 #27
Correction:
Ioannis Vranos wrote:
I managed to make it work under the newer C++ facilities, after having a
look in ISO C++ example code:
The following codes work:
#include <iostream>
#include <locale>
#include <string>
int main()
{
using namespace std;

locale::global( locale("greek") );

wstring ws;

wcin>ws;

wcout<< ws<< endl;
}

Although the above works, I think the correct version is the following:
#include <iostream>
#include <locale>
#include <string>
int main()
{
using namespace std;

locale::global( locale("greek") );

wcin.imbue(loca le());
wcout.imbue(loc ale());

wstring ws;

wcin>ws;

wcout<< ws<< endl;
}
Feb 24 '08 #28
Ioannis Vranos wrote:
Jeff Schwab wrote:
>Ioannis Vranos wrote:
>>I filed a bug in GCC Bugzilla:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35353

Ahh, sanity from the Gnu. :)

>>May anyone explain me how should I apply the suggested solution
"sync_with_st dio (false)", and the fstream suggested solution to the
following failing code?

fstream has nothing to do with it. The Gnu developer just thought you
might have used sync_with_stdio before with file streams. What he
means is that before you do any I/O, you should make a call like this:

std::ios_base:: sync_with_stdio (false);

The following program, as far as I can tell, works find on my system.

#include <iostream>
#include <locale>

int main()
{
std::ios_base:: sync_with_stdio (false);

std::wcout.imbu e(std::locale(" el_GR.UTF-8"));

std::wcout<< L"Î”Î¿ÎºÎ¹Î¼Î±Ï ƒÏ„ικό μήνυμα\n" ;
std::wcout.flus h();
}


Yes it works OK here too. How does desynchronizing with stdio makes the
program work?
No idea. From your other post, the code appears to work without the
sync call.

sync_with_stdio (false) means that the C++ I/O streams no longer have to
coordinate their actions with the C I/O streams: stdin, stdout, and
stderr. The Gnu developer seemed to imply that the C I/O streams had
some fundamental limitation that prevented them to support multi-byte
text. His solution was to "untie" wcout from stdout.
Feb 24 '08 #29
On Sun, 24 Feb 2008 18:15:29 +0200, Ioannis Vranos
<iv*****@nospam .no.spamfreemai l.grwrote:
[...]I am only experimenting with the wide character support. Is there
any possibility the "greek" locale to be only a 8-bit one? And if it is,
how can it also display english (latin characters)? By using something
like ASCII extended (0-127 latin characters and 128-255 greek
characters)?
You might want to use this character set:
http://en.wikipedia.org/wiki/ISO/IEC_8859-7

Boris
Feb 24 '08 #30

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.