vfscanfvfwscanf

从流中读取格式化数据。 提供这些函数的更安全版本;请参阅 vfscanf_svfwscanf_s

语法

int vfscanf(
   FILE *stream,
   const char *format,
   va_list argptr
);
int vfwscanf(
   FILE *stream,
   const wchar_t *format,
   va_list argptr
);

参数

stream
指向 FILE 结构的指针。

format
窗体控件字符串。

arglist
变量参数列表。

返回值

每个函数返回成功转换和分配的字段数。 返回值不包括已读取但未分配的字段。 返回值为 0 表示没有分配任何字段。 如果出现错误或在首次转换前达到文件流的结尾,则 vfscanfvfwscanf 的返回值为 EOF

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

备注

vfscanf 函数将从 stream 的当前位置将数据读取到 arglist 参数列表提供的位置。 列表中的每个参数都必须为指向类型的变量的指针,该类型与 format 中的类型说明符对应。 format 控制输入字段的解释,格式和函数与 scanfformat 参数相同;有关 format 的说明,请参阅 scanf

vfwscanfvfscanf 的宽字符版本;vfwscanf 格式参数是宽字符字符串。 如果在 ANSI 模式下打开流,则这些函数行为相同。 vfscanf 不支持 UNICODE 流的输入。

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_vftscanf vfscanf vfscanf vfwscanf

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

要求

函数 必需的标头
vfscanf <stdio.h>
vfwscanf <stdio.h> 或 <wchar.h>

有关兼容性的详细信息,请参阅 兼容性

示例

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

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

FILE *stream;

int call_vfscanf(FILE * istream, char * format, ...)
{
    int result;
    va_list arglist;
    va_start(arglist, format);
    result = vfscanf(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.out", "w+") != 0)
    {
        printf("The file vfscanf.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(stream, "%s %ld %f%c", s, &l, &fp, &c);

        // 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

另请参阅

流 I/O
_cscanf_cscanf_l_cwscanf_cwscanf_l
fprintf_fprintf_lfwprintf_fwprintf_l
scanf_scanf_lwscanf_wscanf_l
sscanf_sscanf_lswscanf_swscanf_l
fscanf_s_fscanf_s_lfwscanf_s_fwscanf_s_l
vfscanf_svfwscanf_s