fscanf、_fscanf_l、fwscanf、_fwscanf_lfscanf, _fscanf_l, fwscanf, _fwscanf_l

从流中读取带格式的数据。Read formatted data from a stream. 这些函数的更安全版本已发布。请参阅 fscanf_s、_fscanf_s_l、fwscanf_s、_fwscanf_s_lMore secure versions of these functions are available; see fscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l.

语法Syntax

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 ]...
);

参数Parameters

stream
指向文件结构的指针。Pointer to FILE structure.

formatformat
窗体控件字符串。Format-control string.

自变量argument
可选参数。Optional arguments.

localelocale
要使用的区域设置。The locale to use.

返回值Return Value

每个函数都将返回成功转换并分配的字段数;返回值不包括已读取但未分配的字段。Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. 返回值为 0 表示没有分配任何字段。A return value of 0 indicates that no fields were assigned. 如果发生错误,或如果文件流的末尾到达第一个转换,则返回值是EOFfscanffwscanfIf an error occurs, or if the end of the file stream is reached before the first conversion, the return value is EOF for fscanf and fwscanf.

这些函数验证其参数。These functions validate their parameters. 如果格式是 null 指针,则调用无效参数处理程序,如中所述参数验证If stream or format is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则这些函数将返回EOF并设置errnoEINVALIf execution is allowed to continue, these functions return EOF and set errno to EINVAL.

备注Remarks

Fscanf函数将从当前的位置读取数据到提供的位置参数(如果有)。The fscanf function reads data from the current position of stream into the locations given by argument (if any). 每个参数必须是指向的变量的此类型中的类型说明符对应格式Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. 格式控件的输入解释字段,并具有相同形式和函数与格式参数scanf; 请参阅scanf为说明格式format controls the interpretation of the input fields and has the same form and function as the format argument for scanf; see scanf for a description of format.

fwscanf是宽字符版本的fscanf; 的格式参数fwscanf是宽字符字符串。fwscanf is a wide-character version of fscanf; the format argument to fwscanf is a wide-character string. 如果在 ANSI 模式下打开流,则这些函数行为相同。These functions behave identically identically if the stream is opened in ANSI mode. fscanf当前不支持 UNICODE 流中的输入。fscanf doesn't currently support input from a UNICODE stream.

使用这些函数的版本 _l后缀是相同,只不过它们使用传递而不是当前线程区域设置的区域设置参数。The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_ftscanf_ftscanf fscanffscanf fscanffscanf fwscanffwscanf
_ftscanf_l_ftscanf_l _fscanf_l_fscanf_l _fscanf_l_fscanf_l _fwscanf_l_fwscanf_l

有关详细信息,请参阅格式规范字段 — scanf 函数和 wscanf 函数For more information, see Format Specification Fields - scanf functions and wscanf Functions.

要求Requirements

函数Function 必需的标头Required header
fscanf_fscanf_lfscanf, _fscanf_l <stdio.h><stdio.h>
fwscanf_fwscanf_lfwscanf, _fwscanf_l <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

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

请参阅See also

流 I/OStream I/O
_cscanf、_cscanf_l、_cwscanf、_cwscanf_l_cscanf, _cscanf_l, _cwscanf, _cwscanf_l
fprintf、_fprintf_l、fwprintf、_fwprintf_lfprintf, _fprintf_l, fwprintf, _fwprintf_l
scanf、_scanf_l、wscanf、_wscanf_lscanf, _scanf_l, wscanf, _wscanf_l
sscanf、_sscanf_l、swscanf、_swscanf_lsscanf, _sscanf_l, swscanf, _swscanf_l
fscanf_s、_fscanf_s_l、fwscanf_s、_fwscanf_s_lfscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l