The getwc() function is similar to fgetwc(). The main difference between them is getwc() can be implemented as macro.
It is defined in <cwchar> header file.
getwc() Prototype
wint_t getwc(FILE* stream);
The getwc() function takes a file stream stream as its argument and returns the next wide character from the given stream as a value of wide integer type.
getwc() Parameters
- stream: The file stream to read the wide character.
getwc() 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 getwc() function works?
#include <iostream>
#include <cwchar>
#include <cstdio>
#include <clocale>
using namespace std;
int main()
{
wint_t c;
FILE *fp = fopen("file.txt","w+");
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str[] = L"\u0102\u01A5\u01A5\u0139\u011B";// equivalent to ĂƥƥĹě
fputws(str, fp);
rewind(fp);
if (fp)
{
while(!feof(fp))
{
c = getwc(fp);
putwchar(c);
}
}
else
wcout << L"Error opening file" << endl;
fclose(fp);
return 0;
}
When you run the program, a possible output will be:
ĂƥƥĹě