convert wint_t to wchar_t?

R

Rui Maciel

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
 
T

those who know me have no need of my name

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top