Share via


vfscanf, vfwscanf

從資料流讀取格式化資料。 這些函式有更安全的版本可供使用;請參閱 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控制輸入欄位的解譯,並且具有與 formatscanf 變數相同的形式和函式;如需 的描述 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_l, fwprintf, _fwprintf_l
scanf, _scanf_l, wscanf, _wscanf_l
sscanf, _sscanf_l, swscanf, _swscanf_l
fscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l
vfscanf_s, vfwscanf_s