function
<cwchar>

fwprintf

int fwprintf (FILE* stream, const wchar_t* format, ...);
Write formatted data to stream
Writes the C wide string pointed by format to the stream. If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.

After the format parameter, the function expects at least as many additional arguments as specified by format.

The external representation of wide characters in files are multibyte characters: These are obtained as if wcrtomb was called to convert each wide character (using the stream's internal mbstate_t object).

This is the wide character equivalent of fprintf (<cstdio>).

Parameters

stream
Pointer to a FILE object that identifies an output stream.
The stream shall not have an orientation yet, or be wide-oriented (the first i/o operation on a stream determines whether it is byte- or wide- oriented, see fwide).
format
C wide string that contains a format string that follow the same specifications as format in printf but with wide characters (see printf for details).
Notice that all format specifiers have the same meaning as in printf; therefore, %lc shall be used to write a wide character (and not %c), as well as %ls shall be used for wide strings (and not %s).
... (additional arguments)
Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.

Return Value

On success, the total number of characters written is returned.

If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.

If a multibyte character encoding error occurs while writing wide characters, errno is set to EILSEQ and a negative number is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* fwprintf example */
#include <stdio.h>
#include <wchar.h>

int main ()
{
   FILE * pFile;
   int n;
   wchar_t name [100];

   pFile = fopen ("myfile.txt","w");
   for (n=0 ; n<3 ; n++)
   {
     fwprintf (stdout, L"please, enter a name: ");
     fgetws (name, 100, stdin);
     fwprintf (pFile, L"Name %d: %s",n,name);
   }
   fclose (pFile);

   return 0;
}


This example prompts 3 times the user for a name and then writes them to myfile.txt each one in a line with a fixed length (a total of 19 characters + newline).
Two format tags are used:
%d : Signed decimal integer
%-10.10s : left-justified (-), minimum of ten characters (10), maximum of ten characters (.10), string (s).
Assuming that we have entered John, Jean-Francois and Yoko as the 3 names, myfile.txt would contain:

Name 1: John
Name 2: Jean-Franc
Name 3: Yoko

For more examples on formatting see printf.

See also