從資料流讀取格式化資料。 這些函式已有更安全的版本可用,請參閱 vfscanf_s、vfwscanf_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 表示未指派任何欄位。 如果發生錯誤,或進行第一次轉換之前就到達檔案資料流結尾,則 vfscanf 和 vfwscanf 的傳回值是 EOF。
這些函式會驗證它們的參數。 如果 stream 或 format 為 Null 指標,則會叫用無效的參數處理程式,如參數驗證中所述。 如果允許繼續執行,這些函式會傳回 EOF,並將 errno 設為 EINVAL。
備註
vfscanf 函式會將 stream 之目前位置的資料讀入 arglist 引數清單所指定的位置。 清單中的每個引數都必須是變數的指標,而變數的類型對應至 format 中的類型指定名稱。 format控制輸入欄位的解譯,並且具有與 format 自scanf變數相同的形式和函式;如需 的描述format,請參閱 scanf 。
vfwscanf 是 vfscanf 的寬字元版本;vfwscanf 的格式引數是寬字元字串。 如果資料流是以 ANSI 模式開啟,則這些函式的行為相同。 vfscanf 不支援來自 UNICODE 資料流的輸入。
一般文字常式對應
| TCHAR.H 常式 | _UNICODE 和 _MBCS 未定義 |
_MBCS 已定義 |
_UNICODE 已定義 |
|---|---|---|---|
_vftscanf |
vfscanf |
vfscanf |
vfwscanf |
如需詳細資訊,請參閱 格式化規格欄位: scanf 和 wscanf 函式。
需求
| 函式 | 必要的標頭 |
|---|---|
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