fscanf, _fscanf_l, fwscanf, _fwscanf_l

書式付きデータをストリームから読み出します。 これらの関数のセキュリティを強化したバージョンを使用できます。「fscanf_s_fscanf_s_lfwscanf_s_fwscanf_s_l」を参照してください。

構文

int fscanf(
   FILE *stream,
   const char *format [,
   argument ]...
);
int _fscanf_l(
   FILE *stream,
   const char *format,
   _locale_t locale [,
   argument ]...
);
int fwscanf(
   FILE *stream,
   const wchar_t *format [,
   argument ]...
);
int _fwscanf_l(
   FILE *stream,
   const wchar_t *format,
   _locale_t locale [,
   argument ]...
);

パラメーター

stream
FILE 構造体へのポインター。

format
書式指定文字列。

argument
省略可能な引数。

locale
使用するロケール。

戻り値

これらの関数は、正常に変換および代入されたフィールドの数を返します。読み込まれただけで代入されなかったフィールドは戻り値には含まれません。 戻り値が 0 の場合は、代入されたフィールドがなかったことを示します。 エラーが発生した場合や、最初の変換の前にファイル ストリームの終端を検出した場合、EOF および fscanffwscanf を返します。

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

解説

fscanf 関数は、stream の現在位置から argument (ある場合) で指定された位置にデータを読み込みます。 各 argument は、format の型指定子に対応する型の変数へのポインターにする必要があります。 format は、入力フィールドの解釈を制御し、scanf の引数 format と同じ形式と機能を持ちます。format の詳細については、「scanf」を参照してください。

fwscanf 関数は、fscanf 関数のワイド文字バージョンです。fwscanf の format 引数は、ワイド文字列です。 ストリームが ANSI モードで開かれている場合、これらの関数の動作は同じになります。 fscanf では、UNICODE ストリームからの入力はサポートされていません。

これらの関数のうち _l サフィックスが付けられたバージョンは、現在のスレッド ロケールの代わりに渡されたロケール パラメーターを使用する点を除いて同じです。

汎用テキスト ルーチンのマップ

TCHAR.H ルーチン _UNICODE_MBCS が定義されていない _MBCS が定義されている _UNICODE が定義されている
_ftscanf fscanf fscanf fwscanf
_ftscanf_l _fscanf_l _fscanf_l _fwscanf_l

詳細については、「書式指定フィールド: scanf およびwscanf関数」を参照してください

必要条件

機能 必須ヘッダー
fscanf, _fscanf_l <stdio.h>
fwscanf, _fwscanf_l <stdio.h> または <wchar.h>

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

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

#include <stdio.h>

FILE *stream;

int main( void )
{
   long l;
   float fp;
   char s[81];
   char c;

   if( fopen_s( &stream, "fscanf.out", "w+" ) != 0 )
      printf( "The file fscanf.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:
      fscanf( stream, "%s", s );   // C4996
      fscanf( stream, "%ld", &l ); // C4996

      fscanf( stream, "%f", &fp ); // C4996
      fscanf( stream, "%c", &c );  // C4996
      // Note: fscanf is deprecated; consider using fscanf_s instead

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