vsscanf、vswscanfvsscanf, vswscanf

从字符串中读取格式化数据。Reads formatted data from a string. 提供这些函数的更多安全版本;请参阅 vsscanf_s、vswscanf_sMore secure versions of these functions are available; see vsscanf_s, vswscanf_s.


int vsscanf(  
   const char *buffer,  
   const char *format,  
   va_list arglist  
int vswscanf(  
   const wchar_t *buffer,  
   const wchar_t *format,  
   va_list arglist  


存储的数据Stored data

窗体控件字符串。Format-control string. 有关详细信息,请参阅格式规范字段:scanf 和 wscanf 函数For more information, see Format Specification Fields: scanf and wscanf Functions.

变量参数列表。Variable argument list.

返回值Return Value

每个函数都将返回成功转换并分配的字段数;返回值不包括已读取但未分配的字段。Each of these functions returns the number of fields that are successfully converted and assigned; the return value does not include fields that were read but not assigned. 返回值为 0 表示没有分配任何字段。A return value of 0 indicates that no fields were assigned. 如果在首次转换前到达字符串的结尾,则返回值为 EOF 以指示错误。The return value is EOF for an error or if the end of the string is reached before the first conversion.

如果 bufferformatNULL 指针,则调用无效的参数处理程序,如参数验证中所述。If buffer or format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许继续执行,则这些函数返回 -1 并将 errno 设置为 EINVALIf execution is allowed to continue, these functions return -1 and set errno to EINVAL.

有关这些及其他错误代码的信息,请参阅 errno、_doserrno、_sys_errlist 和 _sys_nerrFor information about these and other error codes, see errno, _doserrno, _sys_errlist, and _sys_nerr.


vsscanf 函数将从 buffer 中将数据读取到 arglist 参数列表每个参数提供的位置。The vsscanf function reads data from buffer into the locations that are given by each argument in the arglist argument list. 每个列表中的参数必须为指向类型的变量的指针,该类型与 format 中的类型说明符对应。Every argument in the list must be a pointer to a variable that has a type that corresponds to a type specifier in format. format 参数控制输入字段的解释,并且格式和函数与 scanf 函数的 format 参数相同。The format argument controls the interpretation of the input fields and has the same form and function as the format argument for the scanf function. 如果复制出现在重叠的字符串之间,则该行为不确定。If copying takes place between strings that overlap, the behavior is undefined.


使用 vsscanf 读取字符串时,请始终指定 %s 格式的宽度(例如 "%32s" 而不是 "%s");否则,输入格式不正确会导致缓冲区溢出。When you use vsscanf to read a string, always specify a width for the %s format (for example, "%32s" instead of "%s"); otherwise, incorrectly formatted input can cause a buffer overrun.

vswscanfvsscanf 的宽字符版本;vswscanf 的参数是宽字符串。vswscanf is a wide-character version of vsscanf; the arguments to vswscanf are wide-character strings. vsscanf 不处理多字节十六进制字符。vsscanf does not handle multibyte hexadecimal characters. vswscanf 不处理 Unicode 全角十六进制或“兼容区”字符。vswscanf does not handle Unicode full-width hexadecimal or "compatibility zone" characters. 除此以外,vswscanfvsscanf 的行为完全相同。Otherwise, vswscanf and vsscanf behave identically.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_vstscanf vsscanf vsscanf vswscanf


例程所返回的值Routine 必需的标头Required header
vsscanf <stdio.h><stdio.h>
vswscanf <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// crt_vsscanf.c  
// compile with: /W3  
// This program uses vsscanf to read data items  
// from a string named tokenstring, then displays them.  

#include <stdio.h>  
#include <stdarg.h>  

int call_vsscanf(char *tokenstring, char *format, ...)  
    int result;  
    va_list arglist;  
    va_start(arglist, format);  
    result = vsscanf(tokenstring, format, arglist);  
    return result;  

int main( void )  
    char  tokenstring[] = "15 12 14...";  
    char  s[81];  
    char  c;  
    int   i;  
    float fp;  

    // Input various data from tokenstring:  
    // max 80 character string:  
    call_vsscanf(tokenstring, "%80s", s);  
    call_vsscanf(tokenstring, "%c", &c);  
    call_vsscanf(tokenstring, "%d", &i);  
    call_vsscanf(tokenstring, "%f", &fp);  

    // Output the data read  
    printf("String    = %s\n", s);  
    printf("Character = %c\n", c);  
    printf("Integer:  = %d\n", i);  
    printf("Real:     = %f\n", fp);  
String    = 15  
Character = 1  
Integer:  = 15  
Real:     = 15.000000  

请参阅See Also

流 I/O Stream I/O
scanf、_scanf_l、wscanf、_wscanf_l scanf, _scanf_l, wscanf, _wscanf_l
sscanf、_sscanf_l、swscanf、_swscanf_l sscanf, _sscanf_l, swscanf, _swscanf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_l sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
vsscanf_s、vswscanf_svsscanf_s, vswscanf_s