C++官网参考链接:https://cplusplus.com/reference/cstdio/vsscanf/
函数
<cstdio>
vsscanf
int vsscanf ( const char * s, const char * format, va_list arg );
将格式化数据从字符串读取到可变实参列表中
从s读取数据,并根据形参format将数据存储到由arg标识的可变实参列表中的元素所指向的位置。
在内部,该函数从arg标识的列表中获取实参,就像对它使用了va_arg一样,因此arg的状态很可能被调用改变。
在任何情况下,arg都应该在调用之前的某个时刻由va_start初始化,并期望在调用之后的某个时刻由va_end释放。
形参
s
C字符串,该函数将其处理为获取数据的源。
format
C字符串,它包含一个格式字符串,遵循与scanf中的format相同的规范(详细信息请参阅scanf)。
arg
标识用va_start初始化的可变实参列表的一个值。
va_list是<cstdarg>中定义的特殊类型。
返回值
成功时,函数返回实参列表中成功填充的项数。在匹配失败的情况下,该计数可以与预期的项目数量匹配,或者更少---甚至为0。
如果在成功解释任何数据之前出现输入失败,则返回EOF。
用例
/* vsscanf example */
#include <stdio.h>
#include <stdarg.h>
void GetMatches ( const char * str, const char * format, ... )
{
va_list args;
va_start (args, format);
vsscanf (str, format, args);
va_end (args);
}
int main ()
{
int val;
char buf[100];
GetMatches ( "99 bottles of beer on the wall", " %d %s ", &val, buf);
printf ("Product: %s\nQuantity: %d\n", buf, val);
return 0;
}
可能的输出: