473,695 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wide character input/output

[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 #1
14 4108
Ioannis Vranos <iv*****@nospam .no.spamfreemai l.grwrites:
[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);
You need "%ls". This is very important with wprintf since without it
%s denotes a multi-byte character sequence. printf("%ls\n" input)
should also work. You need the w version if you want the multi-byte
conversion of %s or if the format has to be a wchar_t pointer.
>
return 0;
}
Under Linux:
[john@localhost src]$ ./foobar-cpp
Test
T
[john@localhost src]$
[john@localhost src]$ ./foobar-cpp
Δοκιμασ ικό
�
[john@localhost src]$
The above my not be the only problem. In cases like this, you need to
say way encoding your terminal is using.

<snip>

--
Ben.
Feb 23 '08 #2
Ben Bacarisse wrote:
>
You need "%ls". This is very important with wprintf since without it
%s denotes a multi-byte character sequence. printf("%ls\n" input)
should also work. You need the w version if you want the multi-byte
conversion of %s or if the format has to be a wchar_t pointer.

Perhaps you may help me understand better. We have the usual char
encoding which is implementation defined (usually ASCII).

wchar_t is wide character encoding, which is the "largest character set
supported by the system", so I suppose Unicode under Linux and Windows.

What exactly is a multi-byte character?

I have to say that I am talking about C95 here, not C99.

>
> return 0;
}
Under Linux:
[john@localhost src]$ ./foobar-cpp
Test
T
[john@localhost src]$
[john@localhost src]$ ./foobar-cpp
Δοκιμασ τικό
�
[john@localhost src]$

The above my not be the only problem. In cases like this, you need to
say way encoding your terminal is using.

You are somehow correct on this. My terminal encoding was UTF-8 and I
added Greek(ISO-8859-7). Under the last, the following code works OK:
#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;
}

[john@localhost src]$ ./foobar-cpp
Δοκιμασ ικό
[john@localhost src]$
Also the original, fixed according to your suggestion:
#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;
}

works OK too:

[john@localhost src]$ ./foobar-cpp
Δοκιμασ ικό
Δοκιμασ ικό
[john@localhost src]$
It works OK under Terminal UTF-8 default encoding too. So "%ls" is what
was really needed.
BTW, how can we define UTF-8 as the locale?
Thanks a lot.
Feb 23 '08 #3
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 #4
Ioannis Vranos <iv*****@nospam .no.spamfreemai l.grwrites:
Ben Bacarisse wrote:
>>
You need "%ls". This is very important with wprintf since without it
%s denotes a multi-byte character sequence. printf("%ls\n" input)
should also work. You need the w version if you want the multi-byte
conversion of %s or if the format has to be a wchar_t pointer.


Perhaps you may help me understand better. We have the usual char
encoding which is implementation defined (usually ASCII).

wchar_t is wide character encoding, which is the "largest character
set supported by the system", so I suppose Unicode under Linux and
Windows.

What exactly is a multi-byte character?
It is a confusing term. It means an encoding that uses sequences of
ordinary bytes (in the C sense -- chars) to encode a large character
set. The most common example is UTF-8.
I have to say that I am talking about C95 here, not C99.

>>
>> return 0;
}
Under Linux:
[john@localhost src]$ ./foobar-cpp
Test
T
[john@localhost src]$
[john@localhost src]$ ./foobar-cpp
Δοκιμα τικό

[john@localhost src]$

The above my not be the only problem. In cases like this, you need to
say way encoding your terminal is using.


You are somehow correct on this.
Strange, I know!
My terminal encoding was UTF-8 and I
added Greek(ISO-8859-7). Under the last, the following code works OK:
#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;
}

[john@localhost src]$ ./foobar-cpp
Δοκιμασ ικό
[john@localhost src]$
Also the original, fixed according to your suggestion:
#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;
}

works OK too:

[john@localhost src]$ ./foobar-cpp
Δοκιμασ ικό
Δοκιμασ ικό
[john@localhost src]$
It works OK under Terminal UTF-8 default encoding too. So "%ls" is
what was really needed.
BTW, how can we define UTF-8 as the locale?
I *think* this is now off-topic. I don't think C says anything about
what the locale string means...

The character encoding is usually specified after a '.'. I use, for
example, "en-GB.UTF-8". I suspect that if you only specify a part of
the locale (or one that does not make sense) your C library picks up
what to do from the execution environment. To me "Greek" looks like
an odd locale string. I would expect "el-GR.UTF-8" or
"el-GR.ISO8859-7".

--
Ben.
Feb 23 '08 #5
Ioannis Vranos <iv*****@nospam .no.spamfreemai l.grwrites:
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).
This sort of thing is almost impossible to investigate over Usenet.
Your news software will take your code and may or may not encode the
characters of the L"..." string in the encoding of your post (UTF-8).
It makes it very hard to know what the program text actually is.

Another complication is that the locale setting affects the run-time
behaviour, but you program also depends on what character encoding is
expected by the compiler that builds the string.

--
Ben.
Feb 23 '08 #6
Ben Bacarisse wrote:
>BTW, how can we define UTF-8 as the locale?

I *think* this is now off-topic. I don't think C says anything about
what the locale string means...

The character encoding is usually specified after a '.'. I use, for
example, "en-GB.UTF-8". I suspect that if you only specify a part of
the locale (or one that does not make sense) your C library picks up
what to do from the execution environment. To me "Greek" looks like
an odd locale string. I would expect "el-GR.UTF-8" or
"el-GR.ISO8859-7".

I got the idea from:

http://msdn2.microsoft.com/en-us/lib...1d(VS.80).aspx

http://msdn2.microsoft.com/en-us/lib...zf(VS.80).aspx
Feb 24 '08 #7
Ben Bacarisse wrote:
>BTW, how can we define UTF-8 as the locale?

I *think* this is now off-topic. I don't think C says anything about
what the locale string means...

The character encoding is usually specified after a '.'. I use, for
example, "en-GB.UTF-8". I suspect that if you only specify a part of
the locale (or one that does not make sense) your C library picks up
what to do from the execution environment. To me "Greek" looks like
an odd locale string. I would expect "el-GR.UTF-8" or
"el-GR.ISO8859-7".

This code works with gcc:

#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;
}
[john@localhost src]$ ./foobar-cpp
Δοκιμασ ικό
Δοκιμασ ικό
[john@localhost src]$
When I place el-GR.UTF-8 or el-GR.ISO8859-7 I get:
[john@localhost src]$ ./foobar-cpp
NULL returned!

[john@localhost src]$

Feb 24 '08 #8
Ioannis Vranos <iv*****@nospam .no.spamfreemai l.grwrites:
Ben Bacarisse wrote:
>>BTW, how can we define UTF-8 as the locale?

I *think* this is now off-topic. I don't think C says anything about
what the locale string means...

The character encoding is usually specified after a '.'. I use, for
example, "en-GB.UTF-8". I suspect that if you only specify a part of
the locale (or one that does not make sense) your C library picks up
what to do from the execution environment. To me "Greek" looks like
an odd locale string. I would expect "el-GR.UTF-8" or
"el-GR.ISO8859-7".

I got the idea from:

http://msdn2.microsoft.com/en-us/lib...1d(VS.80).aspx
Ah, OK. Anyway, we are off-topic now. I think you'd have to post in
a Windows group to find out what locale strings mean there.

--
Ben.
Feb 24 '08 #9
Ben Bacarisse wrote:
Ioannis Vranos <iv*****@nospam .no.spamfreemai l.grwrites:
>Ben Bacarisse wrote:
>>>BTW, how can we define UTF-8 as the locale?
I *think* this is now off-topic. I don't think C says anything about
what the locale string means...

The character encoding is usually specified after a '.'. I use, for
example, "en-GB.UTF-8". I suspect that if you only specify a part of
the locale (or one that does not make sense) your C library picks up
what to do from the execution environment. To me "Greek" looks like
an odd locale string. I would expect "el-GR.UTF-8" or
"el-GR.ISO8859-7".
I got the idea from:

http://msdn2.microsoft.com/en-us/lib...1d(VS.80).aspx

Ah, OK. Anyway, we are off-topic now. I think you'd have to post in
a Windows group to find out what locale strings mean there.

I am a Linux user. The "el-GR.UTF-8" and "el-GR.ISO8859-7" you suggested
make setlocale() return NULL. The "greek" and "Greek" suggested by
MSDN works. So I supposed there is a portable way for this. Aren't any
portable locale encoding strings?

Feb 24 '08 #10

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

Similar topics

3
2642
by: Jonathan Mcdougall | last post by:
I started using boost's filesystem library a couple of days ago. In its FAQ, it states "Wide-character names would provide an illusion of portability where portability does not in fact exist. Behavior would be completely different on operating systems (Windows, for example) that support wide-character names, than on systems which don't (POSIX). Providing functionality that appears to provide portability but in fact
2
2009
by: Ashabul Yeameen | last post by:
Hi all, I am writing a C program which at some certain steps needs to use the wide character funcion fputwc() for giving utf-8 output. Since I want to make the code more portable, I wrote my own fputwc() function. But I want to use my own funcion in the case where the compiler does not supply this funcion. Is there any way, i.e. any preprocessor directive, to know whether the supports wide character or not. -Yeameen
11
5205
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The program listed below demonstrates the use of wcsftime() and std::time_put<wchar_t> which is a C++ wrapper around it. (I know this isn't C; but the "problem" lies in the C library implementation of wcsftime()). I'm not sure if this is a platform-dependent feature or part of the C standard. I've compiled with GCC 3.4.3 on GNU/Linux, and run in an en_GB UTF-8
1
2611
by: Andy | last post by:
Can I mix wide and narrow character output to stdout? I seem to remember hearing this was not supported before but I can't find any reference to such a restriction now I actually need to do it! It seems to work on my Intel compiler but is it portable? e.g. #include <iostream> #include <iomanip>
8
11872
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
2
2893
by: Elie Roux | last post by:
Hello, I would like to write a wide chars string with printf, but I do not really understand the behaviour I have with this basic test program for example : #include <stdlib.h> #include <stdio.h> #include <wchar.h> int main () {
1
3601
by: iwongu | last post by:
Hi, I have a question about std::wcout and its underlying C functions. According to C99, a stream have an orientation type that is one of byte-oriented or wide-one, and it is determined by the first using C function on that stream. And the specific orientation functions should not be applied to the stream that have other orientation.
17
10679
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when talking with another team -- codepage -- at the same time. I am more confused when I saw sometimes we need codepage parameter for wide character conversion, and sometimes we do not need for conversion. Here are two examples,
2
1493
by: George2 | last post by:
Hello everyone, I need to know the wide character (unicode) and multibyte (UTF-8) values of a character string of czech. I personally know nothing about czech. Is the following approach correct? 1. I use L on the character string and watch memory to get the wide character representation of the character string in little endian form; 2. I change the computer region/language to czech, and use function WideCharToMultiByte, and use CP_ACP...
0
8635
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8990
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8850
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7664
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6493
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4342
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1977
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.