fscanf, _fscanf_l, fwscanf, _fwscanf_l

스트림에서 형식 지정된 데이터를 읽습니다. 이러한 함수의 더 안전한 버전을 사용할 수 있습니다. 참조 fscanf_s, _fscanf_s_l, fwscanf_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이면 할당된 필드가 없음을 나타냅니다. 오류가 발생하거나 첫 번째 변환 전에 파일 스트림의 끝에 도달할 경우 반환 값은 fscanffwscanf에 대한 EOF입니다.

이러한 함수는 해당 함수 매개 변수의 유효성을 검사합니다. 포인터이거나 format 포인터인 NULL 경우 stream 매개 변수 유효성 검사에 설명된 대로 잘못된 매개 변수 처리기가 호출됩니다. 계속해서 실행하도록 허용된 경우, 이러한 함수는 EOF를 반환하고 errnoEINVAL로 설정합니다.

설명

fscanf 함수는 stream의 현재 위치에서 argument(있는 경우)에 의해 지정된 위치로 데이터를 읽습니다. 각 argumentformat의 형식 지정자에 해당되는 형식의 변수에 대한 포인터여야 합니다. format는 입력 필드의 해석을 제어하고 인수와 동일한 형식과 함수 format 를 가합니다. 에 대한 scanf설명format은 참조하세요scanf.

fwscanffscanf의 와이드 문자 버전이며, fwscanf에 대한 format 인수는 와이드 문자열입니다. 스트림이 ANSI 모드에서 열리는 경우 이러한 함수는 동일하게 작동합니다. fscanf는 현재 UNICODE 스트림에서의 입력을 지원하지 않습니다.

_l 접미사가 있는 이러한 함수 버전은 현재 스레드 로캘 대신 전달된 로캘 매개 변수를 사용하는 경우를 제외하고는 동일합니다.

일반 텍스트 루틴 매핑

TCHAR.H 루틴 _UNICODE 정의 _MBCS 되지 않음 _MBCS 정의 _UNICODE 정의
_ftscanf fscanf fscanf fwscanf
_ftscanf_l _fscanf_l _fscanf_l _fwscanf_l

자세한 내용은 형식 사양 필드 scanfwscanf 함수를 참조하세요.

요구 사항

함수 필수 헤더
fscanf, _fscanf_l <stdio.h>
fwscanf, _fwscanf_l <stdio.h> 또는 <wchar.h>

호환성에 대한 자세한 내용은 호환성을 참조하세요.

예시

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

참고 항목

스트림 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