Share via


vfscanf_s, vfwscanf_s

從資料流讀取格式化資料。 這些版本的 vfscanf、vfwscanf 具有安全性增強功能,如 CRT 中的安全性功能中所述

語法

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

參數

stream
FILE 結構的指標。

format
格式控制字串。

arglist
變數引數清單。

傳回值

每個函式都會傳回成功轉換並指派的欄位數目。 傳回值不包含已讀取但未指派的欄位。 傳回值 0 表示未指派任何欄位。 如果發生錯誤,或進行第一次轉換之前就到達檔案資料流結尾,則 vfscanf_svfwscanf_s 的傳回值是 EOF

這些函式會驗證它們的參數。 如果 stream 是不正確檔案指標,或 format 為 Null 指標,則這些函式會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,這些函式會傳回 EOF,並將 errno 設為 EINVAL

備註

vfscanf_s 函式會將 stream 之目前位置的資料讀入 arglist 引數清單 (如果有的話) 所指定的位置。 清單中的每個引數都必須是變數的指標,而變數的類型對應至 format 中的類型指定名稱。 format 會控制輸入欄位的解譯,並且具有與 formatscanf_s 變數相同的表單和函式;請參閱 格式規格欄位: scanfwscanf 式,以取得 的描述 formatvfwscanf_svfscanf_s 的寬字元版本;vfwscanf_s 的格式引數是寬字元字串。 如果資料流是以 ANSI 模式開啟,則這些函式的行為相同。 vfscanf_s 目前不支援來自 UNICODE 資料流的輸入。

較安全的函式 (具有 _s 字尾) 和其他版本主要的不同在於,較安全的函式要求每個 cCsS[ 類型欄位的大小 (以字元為單位) 都要緊接在變數後以引數的方式傳遞。 如需詳細資訊,請參閱 scanf_swscanf_s_scanf_s_l_wscanf_s_lscanf 寬度規格。

注意

大小參數為型別 unsigned,而非 size_t

泛型文字常式對應

TCHAR.H 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_vftscanf_s vfscanf_s vfscanf_s vfwscanf_s

需求

函式 必要的標頭
vfscanf_s <stdio.h>
vfwscanf_s <stdio.h > 或 < wchar.h>

如需相容性詳細資訊,請參閱相容性

範例

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

另請參閱

資料流 I/O
_cscanf_s, _cscanf_s_l, _cwscanf_s, _cwscanf_s_l
fprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l
scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l
fscanf, _fscanf_l, fwscanf, _fwscanf_l
vfscanf, vfwscanf