### fgetwc -- Read Wide Character from Stream

Format

>     #include <wchar.h>
>     wint_t fgetwc(FILE *stream);

Language Level: ANSI 93
fgetwc reads the next multibyte character from the input stream pointed to by stream, converts it to a wide character, and advances the associated file position indicator for the stream (if defined).

The behavior of fgetwc is affected by the LC_CTYPE category of the current locale. If you change the category between subsequent read operations on the same stream, undefined results can occur.

Using non-wide-character functions with fgetwc on the same stream results in undefined behavior.

After calling fgetwc, flush the buffer or reposition the stream pointer before calling a write function for the stream, unless EOF has been reached. After a write operation on the stream, flush the buffer or reposition the stream pointer before calling fgetwc.

Return Value
fgetwc returns the next wide character that corresponds to the multibyte character from the input stream pointed to by stream. If the stream is at EOF, the EOF indicator for the stream is set and fgetwc returns WEOF.

If a read error occurs, the error indicator for the stream is set and fgetwc returns WEOF. If an encoding error occurs (an error converting the multibyte character into a wide character), fgetwc sets errno to EILSEQ and returns WEOF.

Use ferror and feof to distinguish between a read error and an EOF. EOF is only reached when an attempt is made to read past the last byte of data. Reading up to and including the last byte of data does not turn on the EOF indicator.

Example
This example opens a file, reads in each wide character using fgetwc, and prints out the characters.

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

    int main(void)
    {
       FILE   *stream;
       wint_t wc;

       if (NULL == (stream = fopen("fgetwc.dat", "r"))) {
          printf("Unable to open: \"fgetwc.dat\"\n");
          exit(1);
       }

       errno = 0;
       while (WEOF != (wc = fgetwc(stream)))
          printf("wc = %lc\n", wc);

       if (EILSEQ == errno) {
          printf("An invalid wide character was encountered.\n");
          exit(1);
       }
       fclose(stream);
       return 0;

       /*********************************************************
          Assuming the file fgetwc.dat contains:

          Hello world!

          The output should be similar to:

          wc = H
          wc = e
          wc = l
          wc = l
          wc = o
          :
       *********************************************************/
    }

fgetc -- Read a Character
_fgetchar -- Read Single Character from stdin
fgetws -- Read Wide-Character String from Stream
fputwc -- Write Wide Character
_getch - _getche -- Read Character from Keyboard
<stdio.h>
<wchar.h>