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.

Syntax

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

Parameters

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.

Example:

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
  fclose(pFile);

  //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) {
    putwchar(wc);
    wc = fgetwc(pFile);
  }

  //close the file
  fclose(pFile);
  return 0;
}

The output of the above code will be:

Hello World!

❮ C <wchar.h> Library