vscanfvwscanf

读取标准输入流中的格式化数据。 提供这些函数的更安全版本;请参阅 vscanf_svwscanf_s

语法

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

参数

format
格式控制字符串。

arglist
变量参数列表。

返回值

返回已成功转换和分配的字段数量;返回值不包括已读取但未分配的字段。 返回值为 0 表示没有分配任何字段。

如果 formatNULL 指针,则调用无效参数处理程序,如参数验证中所述。 如果允许执行继续,则这些函数将返回 EOF 并将 errno 设置为 EINVAL

有关这些和其他错误代码的信息,请参阅 errno_doserrno_sys_errlist_sys_nerr

备注

vscanf 函数从标准输入流 stdin 中读取数据,并将数据写入到 arglist 参数列表给定的位置。 列表中的每个参数都必须为指向类型的变量的指针,该类型与 format 中的类型说明符对应。 如果在重叠的字符串之间发生复制,则此行为不确定。

重要

使用 vscanf 读取字符串时,请始终指定 %s 格式的宽度(例如 "%32s" 而不是 "%s");否则,输入格式不正确会导致缓冲区溢出。 作为替代方法,也可以使用 vscanf_svwscanf_sfgets

vwscanfvscanf的宽字符版本; formatvwscanf 参数是宽字符字符串。 如果在 ANSI 模式下打开流,则 vwscanfvscanf 的行为相同。 vscanf 不支持 UNICODE 流的输入。

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_vtscanf vscanf vscanf vwscanf

有关详细信息,请参阅格式规范字段:scanfwscanf 函数

要求

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

通用 Windows 平台 (UWP) 应用中不支持控制台。 与控制台、stdinstdoutstderr 关联的标准流句柄必须重定向,然后 C 运行时函数才能在 UWP 应用中使用它们。 有关兼容性的详细信息,请参阅 兼容性

示例

// 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);
    va_end(arglist);
    return result;
}

int call_vwscanf(wchar_t *format, ...)
{
    int result;
    va_list arglist;
    va_start(arglist, format);
    result = vwscanf(format, arglist);
    va_end(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

另请参阅

数学和浮点支持
流 I/O
区域设置
fscanf_fscanf_lfwscanf_fwscanf_l
printf_printf_lwprintf_wprintf_l
sprintf_sprintf_lswprintf_swprintf_l__swprintf_l
sscanf_sscanf_lswscanf_swscanf_l
vscanf_svwscanf_s