function
<cwchar>
swprintf
int swprintf (wchar_t* ws, size_t len, const wchar_t* format, ...);
Write formatted data to wide string
Composes a wide string with the same text that would be printed if format was used on wprintf, but instead of being printed, the content is stored as a C wide string in the buffer pointed by ws.
If the resulting wide string would be longer than len-1 characters, the remaining characters are discarded and not stored.
A terminating null character is automatically appended after the content.
After the format parameter, the function expects at least as many additional arguments as needed for format.
This is the wide character equivalent of snprintf (<cstdio>).
Parameters
- ws
- Pointer to a buffer where the resulting C wide string is stored.
The buffer should have a size of at least n wide characters.
- len
- Maximum number of wide characters to fill in the ws buffer.
The generated string has a length of at most n-1, leaving space for the additional terminating null wide character.
size_t is an unsigned integral type.
- format
- C wide string that contains a format string that follows the same specifications as format in printf (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. This count does not include the additional null-character automatically appended at the end of the string.
A negative number is returned on failure, including when the resulting string to be written to ws would be longer than n characters.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
/* swprintf example */
#include <stdio.h>
#include <wchar.h>
int main ()
{
wchar_t buffer [100];
int cx;
cx = swprintf ( buffer, 100, L"The half of %d is %d", 80, 80/2 );
swprintf ( buffer+cx, 100-cx-1, L", and the half of that is %d.", 80/2/2 );
fputws ( buffer, stdout );
return 0;
}
|
Output:
The half of 80 is 40, and the half of that is 20.
|
See also
- snprintf
- Write formatted output to sized buffer (function)
- wprintf
- Print formatted data to stdout (function)
- swscanf
- Read formatted data from string (function)