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