C/C中的vswprintf()函数++

vswprintf() 函数将宽字符串写入宽字符串缓冲区。最多 (len-1) 宽字符写入缓冲区,缓冲区后面跟着一个空宽字符。

null

语法:

int-vswprintf(wchar_t*ws、size_t len、const-wchar_t*format、va_list arg)

参数: 该函数接受四个强制参数,如下所述:

  • ws: 指定指向将存储结果的给定宽字符串缓冲区的指针
  • 伦恩: 指定写入缓冲区的宽字符(包括终止的空字符)的最大长度
  • 格式: 指定指向以null结尾的宽字符串的指针
  • arg: 指定标识变量参数列表的值

注: 所有格式说明符的含义与中的相同 printf 因此 %信用证 应用于书写宽字符(而不是 %c ),以及 %ls 应用于宽弦(而不是 %s ).

返回值: 该函数返回两个值,如下所示:

  • 成功时,vswprintf()函数返回写入的宽字符数,不包括终止的空宽字符。
  • 失败时返回负数,包括写入ws的结果字符串长度超过n个字符时。

以下程序说明了上述功能: 项目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" , str);
wprintf(L " %ls " , ws);
return 0;
}


输出:

Some Latin letters : % & ' ( )

项目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" , str);
wprintf(L "%ls" , ws);
return 0;
}


输出:

GFG is : Geek for g

注: 如果生成的字符串长度超过n-1个宽字符,则剩余的字符将被丢弃且不存储。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享