Previous Page
Next Page


Reads a wide character from a file

#include <stdio.h>
#include <wchar.h>
wint_t fgetwc ( 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.


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"),

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)
if ( ferror( fp_in_wide))
  fprintf( stderr, "Error reading the file %s\n", file_in);
if ( ferror( fp_out_wide))
  fprintf( stderr, "Error writing to the file %s\n", file_out);

See Also

getwc( ), getwchar( ), fputwc( ), putwc( ), fgetc( )

Previous Page
Next Page