C++ fgetwc()

The fgetwc() function in C++ reads the next wide character from the given input stream.

The fgetwc() function is defined in <cwchar> header file.

fgetwc() prototype

wint_t fgetwc(FILE* stream);

The fgetwc() function takes a file stream as its argument and returns the next wide character from the given stream as a value of wide integer type.

fgetwc() Parameters

  • stream: The file stream to read the wide character.

fgetwc() Return value

  • On success, the fgetwc() function returns the wide character that is read.
  • On failure it returns WEOF. If an encoding error occurred, sets the errno to EILSEQ.

Example: How fgetwc() function works?

#include <iostream>
#include <cwchar>
#include <cstdio>
#include <clocale>
using namespace std;

int main()
	wint_t c;
	FILE *fp = fopen("file.txt","r+");

	setlocale(LC_ALL, "en_US.UTF-8");
	wchar_t str[] = L"\u0102\u01A5\u01A5\u0139\u011B";// equivalent to ĂƥƥĹě

	fputws(str, fp);
	if (fp)
			c = fgetwc(fp);
	wcout << L"Error opening file" << endl;

	return 0;

When you run the program, a possible output will be:

Did you find this article helpful?