This example opens a file and uses getwc to read wide characters from the file.
#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
:
********************************************************/
}