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.

如果格式NULL调用指针,无效参数处理程序,如中所述参数验证If format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,这些函数将返回EOF并设置errnoEINVALIf 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. 列表中的每个参数必须是指向类型中的类型说明符相对应的变量的指针格式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格式 (例如, "%32 秒" 而不是 "%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.

vwscanf是宽字符版本vscanf;格式参数vwscanf是宽字符字符串。vwscanf is a wide-character version of vscanf; the format argument to vwscanf is a wide-character string. vwscanfvscanf如果在 ANSI 模式下打开流,则行为相同。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_vtscanf vscanfvscanf vscanfvscanf vwscanfvwscanf

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


例程所返回的值Routine 必需的标头Required header
vscanfvscanf <stdio.h><stdio.h>
vwscanfvwscanf <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/OStream I/O
fscanf、_fscanf_l、fwscanf、_fwscanf_lfscanf, _fscanf_l, fwscanf, _fwscanf_l
printf、_printf_l、wprintf、_wprintf_lprintf, _printf_l, wprintf, _wprintf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_lsprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
sscanf、_sscanf_l、swscanf、_swscanf_lsscanf, _sscanf_l, swscanf, _swscanf_l
vscanf_s、vwscanf_svscanf_s, vwscanf_s