Name

fgetwc

Synopsis

Reads a wide character from a file

#include <stdio.h>
#include <wchar.h>
wint_tfgetwc( FILE *fp );

The fgetwc() function reads the wide character at the current file position in the specified file and increments the file position.

The return value of fgetwc() has the type wint_t. If the file position is at the end of the file, or if the end-of-file flag was already set, fgetwc() returns WEOF and sets the end-of-file flag. If a wide-character encoding error occurs, fgetwc() sets the errno variable to EILSEQ (“illegal sequence”) and returns WEOF. Use feof() and ferror() to distinguish errors from end-of-file conditions.

Example

char file_in[ ]  = "local_in.txt",
     file_out[ ] = "local_out.txt";
FILE *fp_in_wide, *fp_out_wide;
wint_t wc;

if ( setlocale( LC_CTYPE, "" ) == NULL)
  fwprintf( stderr,
            L"Sorry, couldn't change to the system's native locale.\n"),
  exit(1);

if (( fp_in_wide = fopen( file_in, "r" )) == NULL )
  fprintf( stderr, "Error opening the file %s\n", file_in), exit(2);

if (( fp_out_wide = fopen( file_out, "w" )) == NULL )
  fprintf( stderr, "Error opening the file %s\n", file_out), exit(3);

fwide( fp_in_wide, 1);           // Not strictly necessary, since first
fwide( fp_out_wide, 1);          // file access also sets wide or byte mode.

while (( wc =fgetwc( fp_in_wide )) != WEOF )
{
  // ... process each wide character read ...

  if ( fputwc( (wchar_t)wc, fp_out_wide) == WEOF) break; } if ( ferror( fp_in_wide)) fprintf( stderr, "Error reading the file %s\n", file_in); if ( ferror( fp_out_wide)) ...

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.