Syntax
#include <stdio.h> #include <wchar.h> wint_t fgetwc(FILE *stream);Description
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.
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.
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.
This example opens a file, reads in each wide character using fgetwc, and prints out the characters.
#include <stdio.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 : ****************************************************************************/ }Related Information