C++ fputwc()

The fputwc() function in C++ writes a wide character to the given output stream.

The fputwc() function is defined in <cwchar> header file.

fputwc() prototype

wint_t fputwc( wchar_t ch, FILE* stream );

The fputwc() function takes a output file stream and a wide character ch as its arguments and writes ch to the file associated with stream.

fputwc() Parameters

  • ch: The wide character to be written.
  • stream: The output file stream to write the wide character.

fputwc() Return value

  • On success, the fputwc() function returns ch.
  • On failure, it returns WEOF. If an encoding error occurs, errno is set to EILSEQ.

Example: How fputwc() function works?

#include <iostream>
#include <cwchar>
#include <cstdio>
#include <clocale>
using namespace std;

int main()
	setlocale(LC_ALL, "en_US.UTF-8");

	/* Devanagiri script */
	wchar_t str[] = L"देवनागरि";
	FILE *fp = fopen("file.txt","w");
	if (fp)
		for(int i=0; i<wcslen(str); i++)
		perror("File opening failed");

	return 0;

When you run the program, the following string will be written to file.txt:

Did you find this article helpful?