vfscanf_s、vfwscanf_svfscanf_s, vfwscanf_s

从流中读取格式化数据。Reads formatted data from a stream. 这些 vfscanf、vfwscanf 版本具有安全增强功能,如 CRT 中的安全功能所述。These versions of vfscanf, vfwscanf have security enhancements, as described in Security Features in the CRT.

语法Syntax

int vfscanf_s(
   FILE *stream,
   const char *format,
   va_list arglist
);
int vfwscanf_s(
   FILE *stream,
   const wchar_t *format,
   va_list arglist
);

参数Parameters

stream
指向文件结构的指针。Pointer to FILE structure.

formatformat
窗体控件字符串。Format-control string.

arglistarglist
变量参数列表。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. 如果发生错误,或如果文件流的末尾到达第一个转换,则返回值是EOFvfscanf_svfwscanf_sIf an error occurs, or if the end of the file stream is reached before the first conversion, the return value is EOF for vfscanf_s and vfwscanf_s.

这些函数验证其参数。These functions validate their parameters. 如果是无效的文件指针,或格式是 null 指针,这些函数将调用无效参数处理程序,如中所述参数验证If stream is an invalid file pointer, or format is a null pointer, these functions invoke the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,则这些函数将返回EOF并设置errnoEINVALIf execution is allowed to continue, these functions return EOF and set errno to EINVAL.

备注Remarks

Vfscanf_s函数将从当前的位置读取数据到提供的位置arglist自变量列表 (如果有)。The vfscanf_s function reads data from the current position of stream into the locations that are given by the arglist argument list (if any). 在列表中的每个自变量必须是指向的变量的此类型中的类型说明符对应格式Each argument in the list must be a pointer to a variable of a type that corresponds to a type specifier in format. 格式控件的输入解释字段,并具有相同形式和函数与格式参数scanf_s; 请参阅格式规范字段:scanf 和 wscanf 函数有关的说明格式format controls the interpretation of the input fields and has the same form and function as the format argument for scanf_s; see Format Specification Fields: scanf and wscanf Functions for a description of format. vfwscanf_s是宽字符版本的vfscanf_s; 的格式参数vfwscanf_s是宽字符字符串。vfwscanf_s is a wide-character version of vfscanf_s; the format argument to vfwscanf_s is a wide-character string. 如果在 ANSI 模式下打开流,则这些函数行为相同。These functions behave identically if the stream is opened in ANSI mode. vfscanf_s当前不支持 UNICODE 流中的输入。vfscanf_s doesn't currently support input from a UNICODE stream.

更安全的函数之间的主要差异 (具有 _s后缀) 和其他版本是更安全的函数,需要以字符为每个单位的大小cCsS,和 [ 类型字段以作为紧跟变量参数传递。The main difference between the more secure functions (that have the _s suffix) and the other versions is that the more secure functions require the size in characters of each c, C, s, S, and [ type field to be passed as an argument immediately following the variable. 有关详细信息,请参阅 scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_lscanf 宽度规范For more information, see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l and scanf Width Specification.

备注

大小参数属于类型无符号,而不size_tThe size parameter is of type unsigned, not size_t.

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

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_vftscanf_s_vftscanf_s vfscanf_svfscanf_s vfscanf_svfscanf_s vfwscanf_svfwscanf_s

要求Requirements

函数Function 必需的标头Required header
vfscanf_svfscanf_s <stdio.h><stdio.h>
vfwscanf_svfwscanf_s <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

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

示例Example

// crt_vfscanf_s.c
// compile with: /W3
// This program writes formatted
// data to a file. It then uses vfscanf_s to
// read the various data back from the file.

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

FILE *stream;

int call_vfscanf_s(FILE * istream, char * format, ...)
{
    int result;
    va_list arglist;
    va_start(arglist, format);
    result = vfscanf_s(istream, format, arglist);
    va_end(arglist);
    return result;
}

int main(void)
{
    long l;
    float fp;
    char s[81];
    char c;

    if (fopen_s(&stream, "vfscanf_s.out", "w+") != 0)
    {
        printf("The file vfscanf_s.out was not opened\n");
    }
    else
    {
        fprintf(stream, "%s %ld %f%c", "a-string",
            65000, 3.14159, 'x');
        // Security caution!
        // Beware loading data from a file without confirming its size,
        // as it may lead to a buffer overrun situation.

        // Set pointer to beginning of file:
        fseek(stream, 0L, SEEK_SET);

        // Read data back from file:
        call_vfscanf_s(stream, "%s %ld %f%c", s, _countof(s), &l, &fp, &c, 1);

        // Output data read:
        printf("%s\n", s);
        printf("%ld\n", l);
        printf("%f\n", fp);
        printf("%c\n", c);

        fclose(stream);
    }
}
a-string
65000
3.141590
x

请参阅See also

流 I/OStream I/O
_cscanf_s、_cscanf_s_l、_cwscanf_s、_cwscanf_s_l_cscanf_s, _cscanf_s_l, _cwscanf_s, _cwscanf_s_l
fprintf_s、_fprintf_s_l、fwprintf_s、_fwprintf_s_lfprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l
scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_lscanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
sscanf_s、_sscanf_s_l、swscanf_s、_swscanf_s_lsscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l
fscanf、_fscanf_l、fwscanf、_fwscanf_lfscanf, _fscanf_l, fwscanf, _fwscanf_l
vfscanf、vfwscanfvfscanf, vfwscanf