By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

convert wint_t to wchar_t?

P: n/a
I'm trying to use the getwc() function to get individual characters from a
stream but I've stumbled on a type conversion problem. The getwc() function
returns a type wint_t instead of a wchar_t and there isn't any information
on how to convert a wint_t type to wchar_t and vice-versa.

As the purpose of the wint_t type in that particular case is to serve as a
data type capable of storing all the possible values held by wchar_t along
with the WEOF value, I suspect that a simple cast could do the trick. Is
this true or could that approach break something?
Thanks in advance
Rui Maciel
Jul 3 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
in comp.lang.c i read:
>I'm trying to use the getwc() function to get individual characters from a
stream but I've stumbled on a type conversion problem. The getwc() function
returns a type wint_t instead of a wchar_t and there isn't any information
on how to convert a wint_t type to wchar_t and vice-versa.
wchar_t and wint_t are the wide varieties of char and int, for character
handling purposes at least. assignment is sufficient -- you don't cast
char to int do you? -- and often you want wint_t rather than wchar_t
anyway, i.e., just as with getc you want to assign to an int not a char
likewise with getwc you want to assign to a wint_t not a wchar_t ...

basic "cat", narrow:

int c;

while (EOF != (c = getc(stdin)))
putc(c, stdout);

now wide:

wint_t c;

while (WEOF != (c = getwc(stdin)))
putwc(c, stdout);

remove repeated spaces, narrow:

int c, lastc = 0;

while (EOF != (c = getc(stdin))) {
if (' ' != c || c != lastc) putc(c, stdout);
lastc = c;
}

and wide:

wint_t c, lastc = 0;

while (WEOF != (c = getwc(stdin))) {
if (L' ' != c || c != lastc) putwc(c, stdout);
lastc = c;
}

surely there are many aspects that go beyond this[*], but at the level you
are asking about the same handling you do today with char and int works
equally well with wchar_t and wint_t. no casts needed.
[*] the first among many is that for wide characters to be effective you
must invoke setlocale() prior to using wide functions ...

#include <stdio.h>

int main(void) {
int c, lastc = 0;

while (EOF != (c = getc(stdin))) {
if (' ' != c || c != lastc) putc(c, stdout);
lastc = c;
}
return 0;
}

must needs expand to something more like:

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

int main(void) {
wint_t c, lastc = 0;

if (!setlocale(LC_CTYPE, "")) abort(); /* only character classification */

while (WEOF != (c = getwc(stdin))) {
if (L' ' != c || c != lastc) putwc(c, stdout);
lastc = c;
}
return 0;
}

--
a signature
Jul 3 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.