C++ <cwchar> - vfwprintf() Function
The C++ <cwchar> vfwprintf() function writes the C wide string pointed by format to the stream. If format includes format specifiers (sub-sequences beginning with %), the variable argument list identified by vlist are formatted and inserted in the resulting string replacing their respective specifiers.
Internally, the function retrieves arguments from the list identified by vlist as if va_start was used on it, and thus the state of vlist is likely altered by the call.
In any case, vlist should have been initialized by va_start at some point before the call, and it is expected to be released by va_end at some point after the call.
The multibyte characters in file are interpreted as wide characters as if wcrtomb() function is called to convert each wide character (using the stream's internal mbstate_t object).
Syntax
int vfwprintf ( FILE * stream, const wchar_t* format, va_list vlist );
Parameters
stream |
Specify a pointer to a FILE object that specifies an output stream. | ||||||||||||||||||||||||||||||||||||||||
format |
Specify the format wide string. The possible values of %specifier are:
Additional format values can be placed between the % and the specifier (e.g. %.3f) also known as sub-specifier. These sub-specifier are:
Note: If multiple additional format values are provided, they must be in %[flags][width][.precision][length]specifier order. | ||||||||||||||||||||||||||||||||||||||||
vlist |
Specify a variable argument list containing the data to print initialized with va_start. |
Return Value
Returns the total number of characters written on success (not including the terminating null character). If a writing error occurs, sets the error indicator ferror() 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: vfwprintf() example
In the example below shows the usage of vfwprintf() function.
#include <cwchar> #include <cstdio> #include <cstdarg> void WriteWideFormatted( FILE * stream, const wchar_t * format, ... ) { va_list args; va_start(args, format); vfwprintf(stream, format, args); va_end(args); } int main () { //open the file in write and read mode FILE *pFile = fopen("test.txt", "w+"); WriteWideFormatted(pFile, L"Calling with %d variable argument.\n", 1); WriteWideFormatted(pFile, L"Calling with %d variable %ls.\n", 2, L"arguments"); WriteWideFormatted(pFile, L"Calling with %d %ls %ls.\n", 3, L"variable", L"arguments"); //set the position to the start rewind(pFile); //display the content of the file 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:
Calling with 1 variable argument. Calling with 2 variable arguments. Calling with 3 variable arguments.
Length sub-specifier
Specifiers | |||||||
---|---|---|---|---|---|---|---|
length | d i | u o x X | f F e E g G a A | c | s | p | n |
(none) | int | unsigned int | double | int | char* | void* | int* |
hh | signed char | unsigned char | signed char* | ||||
h | short int | unsigned short int | short int* | ||||
l | long int | unsigned long int | double | wint_t | wchar_t* | long int* | |
ll | long long int | unsigned long long int | long long int* | ||||
j | intmax_t | uintmax_t | intmax_t* | ||||
z | size_t | size_t | size_t* | ||||
t | ptrdiff_t | ptrdiff_t | ptrdiff_t* | ||||
L | long double |
See <cinttypes> for the specifiers for extended types.
❮ C++ <cwchar> Library