convert wint_t to wchar_t?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rui Maciel

    convert wint_t to wchar_t?

    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
  • those who know me have no need of my name

    #2
    Re: convert wint_t to wchar_t?

    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

    Comment

    Working...