### fwprintf -- Format Data as Wide Characters and Write to a Stream

Format

>     #include <stdio.h>
>     #include <wchar.h>
>     int fwprintf(FILE *stream, const wchar_t *format, argument-list);

Language Level: ANSI 93
fwprintf writes output to the stream pointed to by stream, under control of the wide string pointed to by format. The format string specifies how subsequent arguments are converted for output.

fwprintf converts each entry in the argument-list according to the corresponding wide-character format specifier in format.

If insufficient arguments exist for the format, the behavior is undefined. If the format is exhausted while arguments remain, fwprintf evaluates, as usual, the excess arguments, but otherwise ignores them. fwprintf returns when it encounters the end of the format string.

The format comprises zero or more directives (ordinary wide characters -- not % -- and conversion specifications). Conversion specifications are processed as if they were replaced in the format string by wide-character strings, each the result of fetching zero or more subsequent arguments and then converting them, if applicable, according to the corresponding conversion specifier. fwprintf then writes the expanded wide-character format string to the output stream.

The format for fwprintf has the same form and function as the format string for printf, with the following exceptions:

- %c (without an l prefix) converts an integer argument to wchar_t, as if by calling btowc.
- %s (without an l prefix) converts an array of multibyte characters to an array of wchar_t, as if by calling mbrtowc. The array is written up to, but not including, the terminating null character, unless the precision specifies a shorter output.
- %ls and %S write an array of wchar_t. The array is written up to, but not including, the terminating null character, unless the precision specifies a shorter output.

For a complete description of format specifiers, see printf -- Print Formatted Characters.

If a conversion specification is invalid, the behavior is undefined.

If any argument is, or points to, an union or an aggregate (except for an array of char type using %s conversion, an array of wchar_t type using %ls conversion, or a pointer using %p conversion), the behavior is undefined.

In no case does a nonexistent, or small field width, cause truncation of a field; if the conversion result is wider than the field width, the field is expanded to contain the conversion result.

Return Value
fwprintf returns the number of wide characters transmitted.

If an output error occurred, fwprintf returns a negative value.

Example
This example prints date and time in the form "Monday, April 14, 17:47" followed by pi to five decimal places. Note that date and time may be formatted according to your locale's representation. The output goes to a file.

    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <time.h>
    #include <wchar.h>
    #include <locale.h>

    int main(void)
    {
       wchar_t *weekday, *month;
       FILE *stream;
       struct tm *newtime;
       time_t ltime;
       struct dtconv *p;

       weekday = (wchar_t *)malloc(sizeof(wchar_t)*20);
       month = (wchar_t *)malloc(sizeof(wchar_t)*20);

       stream = fopen("myfile.dat", "w");    /* Open the stream for writing */

       setlocale(LC_ALL,"");

       time(&ltime);
       newtime = localtime(&ltime);
       p = localdtconv();
       mbstowcs(weekday, p->day_names[newtime->tm_wday], sizeof(wchar_t)*20);
       mbstowcs(month, p->month_names[newtime->tm_mon], sizeof(wchar_t)*20);

       fwprintf(stream, L"%ls, %ls %d, %.2d:%.2d\n",
                weekday, month, newtime->tm_mday,
                newtime->tm_hour, newtime->tm_min);

       fwprintf(stream, L"pi = %.5f\n", 4* atan(1.0));

       fclose(stream);
       return 0;

       /*******************************************************************
          The contents of output file myfile.dat should be similar to:

          Monday, April 14, 17:47
          pi = 3.15149
       *******************************************************************/
    }

fprintf -- Write Formatted Data to a Stream
printf -- Print Formatted Characters
vfprintf -- Print Argument Data to Stream
vprintf -- Print Argument Data
btowc -- Convert Single Byte to Wide Character
mbrtowc -- Convert Multibyte Character to Wide Character
vfwprintf -- Format Argument Data as Wide Characters and Write to a Stream
vswprintf -- Format and Write Wide Characters to Buffer
vwprintf -- Format Argument Data as Wide Characters and Print
wprintf -- Format Data as Wide Characters and Print
<stdarg.h>
<wchar.h>