The fgetwc
function reads a wide character from stream and returns it. To use this function, the wchar.h
header file needs to be included as shown below:
#include <wchar.h>
This function is written as shown below:
wint_t fgetwc( FILE *stream );
stream
- pointer to a FILE
that identifies a stream from which the character is read.
This function returns the wide-character that is read from the stream as type wint_t
in case of success. Otherwise, it returns WEOF
. WEOF
indicated end-of-file or some other error.
The following code shows the usage of the fgetwc
function. It reads a file sampleFile.txt
, stores its content in wideChar
using fgetwc
, and counts the number of a
letters in the file until the counter n
reaches the end of the file:
#include <stdio.h>#include <wchar.h>int main (){FILE * thisFile;wint_t wideChar;int n = 0;thisFile=fopen ("sampleFile.txt","r");if (thisFile!=NULL){do {wideChar = fgetwc (thisFile);if (wideChar == L'a') n++;} while (wideChar != WEOF);fclose (thisFile);wprintf (L"The file contains %d a letters ($).\n",n);}return 0;}
Free Resources