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 の場合は、代入されたフィールドがなかったことを示します。 エラーが発生した場合や、最初の変換の前にファイル ストリームの終端を検出した場合、EOF および vfscanf_svfwscanf_s を返します。

これらの関数では、パラメーターの検証が行われます。 無効なファイル ポインターまたは format null ポインターの場合stream、「パラメーターの検証」で説明されているように、これらの関数は無効なパラメーター ハンドラーを呼び出します。 実行の継続が許可された場合、これらの関数は EOF を返し、errnoEINVAL に設定します。

解説

vfscanf_s 関数は、stream の現在位置から arglist 引数リスト (ある場合) で指定された位置にデータを読み込みます。 リストの各引数は、format の型指定子に対応する型の変数へのポインターにする必要があります。 formatは、入力フィールドの解釈を制御し、引数と同じ形式と関数をformat持ちます。の説明formatについては、「書式指定フィールド: scanf およびwscanf関数」を参照してくださいscanf_s vfwscanf_s 関数は、vfscanf_s 関数のワイド文字バージョンです。vfwscanf_s の format 引数は、ワイド文字列です。 ストリームが ANSI モードで開かれている場合、これらの関数の動作は同じになります。 vfscanf_s では、UNICODE ストリームからの入力はサポートされていません。

セキュリティが強化された関数 (_s サフィックス付き) とその他のバージョンの関数との主な違いは、セキュリティが強化された関数では、cCsS、および [ の各型フィールドを使用するとき、引数として、それぞれの変数の直後にフィールドのサイズを渡す必要がある点です。 詳細については、「、、_scanf_s_l、、wscanf_s_wscanf_s_lおよび幅のscanf指定」を参照してくださいscanf_s

Note

サイズ パラメーターは 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>

互換性の詳細については、「 Compatibility」を参照してください。

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

関連項目

ストリーム入出力
_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