vscanf、vwscanfvscanf, vwscanf

读取标准输入流中的格式化数据。Reads formatted data from the standard input stream. 提供这些函数的更多安全版本;请参阅 vscanf_s、vwscanf_sMore secure versions of these function are available; see vscanf_s, vwscanf_s.


int vscanf(  
   const char *format,  
   va_list arglist  
int vwscanf(  
   const wchar_t *format,  
   va_list arglist  


格式控制字符串。Format control string.

变量参数列表。Variable argument list.

返回值Return Value

返回已成功转换和分配的字段数量;返回值不包括已读取但未分配的字段。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.

如果 formatNULL 指针,则调用无效参数处理程序,如参数验证中所述。If format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则这些函数将返回 EOF 并将 errno 设置为 EINVALIf execution is allowed to continue, these functions return EOF 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.


vscanf 函数从标准输入流 stdin 中读取数据,并将数据写入到 arglist 参数列表给定的位置。The vscanf function reads data from the standard input stream stdin and writes the data into the locations that are given by the arglist argument list. 列表中的每个参数都必须为指向类型的变量的指针,该类型与 format 中的类型说明符对应。Each argument in the list must be a pointer to a variable of a type that corresponds to a type specifier in format. 如果在重叠的字符串之间发生复制,则此行为不确定。If copying occurs between strings that overlap, the behavior is undefined.


使用 vscanf 读取字符串时,请始终指定 %s 格式的宽度(例如 "%32s" 而不是 "%s");否则,输入格式不正确会导致缓冲区溢出。When you use vscanf 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. 可以使用 vscanf_s、vwscanf_sfgets 作为替代方法。As an alternative, you can use vscanf_s, vwscanf_s or fgets.

vwscanfvscanf 的宽字符版本;formatvwscanf 参数是宽字符字符串。vwscanf is a wide-character version of vscanf; the format argument to vwscanf is a wide-character string. 如果在 ANSI 模式下打开流,则 vwscanfvscanf 的行为相同。vwscanf and vscanf behave identically if the stream is opened in ANSI mode. vscanf 不支持 UNICODE 流的输入。vscanf doesn't support input from a UNICODE stream.

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

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_vtscanf vscanf vscanf vwscanf

有关详细信息,请参阅格式规范字段:scanf 和 wscanf 函数For more information, see Format Specification Fields: scanf and wscanf Functions.


例程Routine 必需的标头Required header
vscanf <stdio.h><stdio.h>
vwscanf <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

通用 Windows 平台 (UWP) 应用中不支持控制台。The console is not supported in Universal Windows Platform (UWP) apps. 控制台中,与关联的标准流句柄stdinstdout,和stderr,必须将重定向,然后 C 运行时函数可以在 UWP 应用中使用它们。The standard stream handles that are associated with the console, stdin, stdout, and stderr, must be redirected before C run-time functions can use them in UWP apps. 有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// crt_vscanf.c  
// compile with: /W3  
// This program uses the vscanf and vwscanf functions  
// to read formatted input.  

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

int call_vscanf(char *format, ...)  
    int result;  
    va_list arglist;  
    va_start(arglist, format);  
    result = vscanf(format, arglist);  
    return result;  

int call_vwscanf(wchar_t *format, ...)  
    int result;  
    va_list arglist;  
    va_start(arglist, format);  
    result = vwscanf(format, arglist);  
    return result;  

int main( void )  
    int   i, result;  
    float fp;  
    char  c, s[81];  
    wchar_t wc, ws[81];  
    result = call_vscanf( "%d %f %c %C %80s %80S", &i, &fp, &c, &wc, s, ws );  
    printf( "The number of fields input is %d\n", result );  
    printf( "The contents are: %d %f %c %C %s %S\n", i, fp, c, wc, s, ws);  
    result = call_vwscanf( L"%d %f %hc %lc %80S %80ls", &i, &fp, &c, &wc, s, ws );  
    wprintf( L"The number of fields input is %d\n", result );  
    wprintf( L"The contents are: %d %f %C %c %hs %s\n", i, fp, c, wc, s, ws);  

      71 98.6 h z Byte characters  
36 92.3 y n Wide charactersThe number of fields input is 6  
The contents are: 71 98.599998 h z Byte characters  
The number of fields input is 6  
The contents are: 36 92.300003 y n Wide characters  

请参阅See Also

浮点支持 Floating-Point Support
流 I/O Stream I/O
区域设置 Locale
fscanf、_fscanf_l、fwscanf、_fwscanf_l fscanf, _fscanf_l, fwscanf, _fwscanf_l
printf、_printf_l、wprintf、_wprintf_l printf, _printf_l, wprintf, _wprintf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_l sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
sscanf、_sscanf_l、swscanf、_swscanf_l sscanf, _sscanf_l, swscanf, _swscanf_l
vscanf_s、vwscanf_svscanf_s, vwscanf_s