C Standard Library

C <wchar.h> - fputws() Function

The C <wchar.h> fputws() function writes every character of the null-terminated wide string pointed to by ws to the output stream stream, until it reaches the terminating null wide character. The terminating null wide character is not copied to the stream.


int fputws (const wchar_t* ws, FILE* stream);


ws Specify a null-terminated wide string to be written.
stream Specify a pointer to a FILE object that specifies an output stream.

Return Value

On success, returns a non-negative value. On failure, returns EOF and sets the error indicator ferror() on stream.


In the example below, the file test.txt is opened in write mode.The program creates an empty file for output operations if the file does not exist. If the file already exists, its contents are discarded and the file is treated as a new empty file. Finally it writes null-terminated wide string in the file before closing it.

#include <wchar.h>
#include <stdio.h>

int main (){
  //open the file in write mode
  FILE *pFile = fopen("test.txt", "w");
  //writes null-terminated wide 
  //string in the file
  wchar_t ws[100] = L"Hello World!";
  fputws(ws, pFile);
  //close the file

  //open the file in read mode to read
  //the content of the file
  pFile = fopen("test.txt", "r");
  wint_t wc = fgetwc(pFile);
  while (wc != WEOF) {
    wc = fgetwc(pFile);

  //close the file
  return 0;

The output of the above code will be:

Hello World!

❮ C <wchar.h> Library