Open main menu

Wikibooks β

C Programming/wchar.h/fgetws

< C Programming‎ | wchar.h(Redirected from C Programming/C Reference/wchar.h/fgetws)

fgetws is a function in the C programming language. It is wide-character version of the function fgets. The w in fgetws is for wide. A string is read as a multibyte-character or a wide character string by fgetws depending on whether the stream is opened in text/binary mode respectively. The fgetws subroutine reads characters from the input stream, converts them to the corresponding wide character codes, and places them in the array pointed to by the string parameter.

The subroutine continues until either:

  • The number of characters specified by the number parameter '-1' are read
  • The subroutine encounters a newline or EOF character.

The fgetws subroutine terminates the wide character string with a NULL wide character.



#include <stdio.h>
#include <wchar.h>
wchar_t *fgetws( 
    wchar_t *string;
    int n;
    FILE *stream ;


fgetws has three parameters:

  1. string - a string used to provide storage location for data
  2. n - the maximum number of readable characters
  3. stream - a FILE pointer


Although fgetws is wider relative to fgets, it can be compiled by an additional optional header along with stdio.h called wchar.h. However, fgets requires stdio.h compulsorily. Hence, fgetws provides option.

Return valueEdit

Just as fgets function, fgetws function also returns the same value string i.e. a ws is returned on success. The error condition is taken care-of by this function using a NULL pointer. A Null pointer is returned to the function-called for error or even at EOF(end of file). One can use also use the feof or ferror for error determination.


ubuntu, fedora, ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP