This vswprintf() function writes the wide string to the wide string buffer. A maximum of (len-1) wide characters are written to buffer which is followed by a null wide character.
Syntax:
int vswprintf( wchar_t* ws, size_t len, const wchar_t* format, va_list arg )
Parameters: The function accepts four mandatory parameters which are described below:
- ws: specifies the pointer to a given wide string buffer which will store the result
- len: specifies the maximum length of wide characters written back to the buffer including the terminating null character
- format: specifies the pointer to a null terminated wide string
- arg: specifies the value identifying a variable arguments list
Note: 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 ).
Return value: The function returns two value as below:
- On success, the vswprintf() function returns number of wide characters written excluding the terminating null wide character.
- A negative number is returned on failure, including when the resulting string to be written to ws would be longer than n characters.
Below programs illustrate the above function:
Program 1 :
// C++ program to illustrate the // vswprintf() function #include <bits/stdc++.h> using namespace std;
// function to check the number // of wide characters written void find ( wchar_t * ws, size_t len, const wchar_t *format, ... )
{ // hold the variable argument
va_list arg;
// A function that invokes va_start
// shall also invoke va_end before it returns.
va_start ( arg, format );
vswprintf ( ws, len, format, arg );
va_end ( arg );
} // Driver code int main ()
{ // buffer with size 60
wchar_t ws[60];
// initializing the string as latin characters
wchar_t str[] = L "\u0025 \u0026 \u0027 \u0028 \u0029" ;
// print the letters
find(ws, 60, L "Some Latin letters : %ls\n" , str);
wprintf(L " %ls " , ws);
return 0;
} |
Some Latin letters : % & ' ( )
Program 2 :
// C++ program to illustrate the // vswprintf() function // When the size of the buffer is smaller // than the total length of the string written #include <bits/stdc++.h> using namespace std;
// function to check the number // of wide characters written void find ( wchar_t * ws, size_t len, const wchar_t *format, ... )
{ // hold the variable argument
va_list arg;
// A function that invokes va_start
// shall also invoke va_end before it returns.
va_start ( arg, format );
vswprintf ( ws, len, format, arg );
va_end ( arg );
} // Driver code int main ()
{ // initializing the string as english characters
wchar_t str[] = L "Geek for geeks" ;
// buffer with size 20
wchar_t ws[20];
find(ws, 20, L "GFG is : %ls\n" , str);
wprintf(L "%ls" , ws);
return 0;
} |
GFG is : Geek for g
Note: If the resulting string would be longer than n-1 wide characters, the remaining characters are discarded and not stored.