vswprintf() function in C/C++
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 :
#include <bits/stdc++.h>
using namespace std;
void find ( wchar_t * ws, size_t len, const wchar_t *format, ... )
{
va_list arg;
va_start ( arg, format );
vswprintf ( ws, len, format, arg );
va_end ( arg );
}
int main ()
{
wchar_t ws[60];
wchar_t str[] = L "\u0025 \u0026 \u0027 \u0028 \u0029" ;
find(ws, 60, L "Some Latin letters : %ls\n" , str);
wprintf(L " %ls " , ws);
return 0;
}
|
Output:
Some Latin letters : % & ' ( )
Program 2 :
#include <bits/stdc++.h>
using namespace std;
void find ( wchar_t * ws, size_t len, const wchar_t *format, ... )
{
va_list arg;
va_start ( arg, format );
vswprintf ( ws, len, format, arg );
va_end ( arg );
}
int main ()
{
wchar_t str[] = L "Geek for geeks" ;
wchar_t ws[20];
find(ws, 20, L "GFG is : %ls\n" , str);
wprintf(L "%ls" , ws);
return 0;
}
|
Output:
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.
Last Updated :
18 Sep, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...