Share via


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 表示字段未分配。 如果出现错误,或者,如果文件流的末尾在第一个转换之前到达,则返回值是 fscanf 和 fwscanf的 EOF 。

这些功能验证其参数。 如果 stream 或 format 是 null 指针,无效参数调用处理程序,如 参数验证所述。 如果执行允许继续,这些函数返回 EOF 并将 errno 到 EINVAL。

备注

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

有关其他的兼容性信息,请参见中介绍的 兼容性

示例

// 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 );
   }
}
  

.NET Framework 等效项

系统:: IO:: StreamReader:: ReadLine。请参见 Parse 方法,如 系统:: 二进制文件:: 分析

请参见

参考

流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