Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is fgetwc in C ?

Sheza Naveed

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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>

Prototype

This function is written as shown below:

wint_t fgetwc( FILE *stream );

Parameters

stream - pointer to a FILE that identifies a stream from which the character is read.

Return value

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.

Example

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:

main.c
sampleFile.txt
#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;
}

RELATED TAGS

c

CONTRIBUTOR

Sheza Naveed
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring