fscanf_s、_fscanf_s_l、fwscanf_s、_fwscanf_s_lfscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l

从流中读取格式化数据。Reads formatted data from a stream. 这些版本的 fscanf、_fscanf_l、fwscanf、_fwscanf_l_l 具有安全增强功能,如 CRT 中的安全增强功能所述。These versions of fscanf, _fscanf_l, fwscanf, _fwscanf_l have security enhancements, as described in Security Features in the CRT.


int fscanf_s(   
   FILE *stream,  
   const char *format [,  
   argument ]...   
int _fscanf_s_l(   
   FILE *stream,  
   const char *format,  
   locale_t locale [,  
   argument ]...   
int fwscanf_s(   
   FILE *stream,  
   const wchar_t *format [,  
   argument ]...   
int _fwscanf_s_l(   
   FILE *stream,  
   const wchar_t *format,  
   locale_t locale [,  
   argument ]...   


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

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

可选参数。Optional arguments.

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

返回值Return Value

每个函数都将返回成功转换并分配的字段数;返回值不包括已读取但未分配的字段。Each of these functions returns the number of fields that are 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. 如果出现错误或在首次转换前达到文件流的结尾,则 fscanf_sfwscanf_s 的返回值为 EOFIf an error occurs, or if the end of the file stream is reached before the first conversion, the return value is EOF for fscanf_s and fwscanf_s.

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


fscanf_s 函数将从 stream 的当前位置将数据读取到 argument(如果有)指定的位置。The fscanf_s function reads data from the current position of stream into the locations that are given by argument (if any). 每个 argument 必须为指向类型的变量的指针,该类型与 format 中的类型说明符对应。Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. format 控制输入字段的解释,格式和函数与 scanf_sformat 参数相同;有关 format 的说明,请参阅格式规范字段:scanf 和 wscanf 函数format controls the interpretation of the input fields and has the same form and function as the format argument for scanf_s; see Format Specification Fields: scanf and wscanf Functions for a description of format. fwscanf_sfscanf_s 的宽字符版本;fwscanf_s 格式参数是宽字符字符串。fwscanf_s is a wide-character version of fscanf_s; the format argument to fwscanf_s is a wide-character string. 如果在 ANSI 模式下打开流,则这些函数行为相同。These functions behave identically if the stream is opened in ANSI mode. fscanf_s 当前不支持 UNICODE 流的输入。fscanf_s doesn't currently support input from a UNICODE stream.

更安全的函数(带有 _s 后缀)与其他版本之间的主要区别在于,更安全的函数需要每个 cCsS[ 类型字段的以字符为单位的大小作为紧跟变量的参数进行传递。The main difference between the more secure functions (that have the _s suffix) and the other versions is that the more secure functions require the size in characters of each c, C, s, S, and [ type field to be passed as an argument immediately following the variable. 有关详细信息,请参阅 scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_lscanf 宽度规范For more information, see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l and scanf Width Specification.


大小参数的类型具有 unsigned,而不具有 size_tThe size parameter is of type unsigned, not size_t.

这些带有 _l 后缀的函数的版本相同,只不过它们使用传递的区域设置参数而不是当前线程区域设置。The versions of these functions that have the _l suffix are identical except that they use the locale parameter that's 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_s fscanf_s fscanf_s fwscanf_s
_ftscanf_s_l _fscanf_s_l _fscanf_s_l _fwscanf_s_l


函数Function 必需的标头Required header
fscanf_s, _fscanf_s_lfscanf_s, _fscanf_s_l <stdio.h><stdio.h>
fwscanf_s, _fwscanf_s_lfwscanf_s, _fwscanf_s_l <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

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


// crt_fscanf_s.c  
// This program writes formatted  
// data to a file. It then uses fscanf to  
// read the various data back from the file.  

#include <stdio.h>  
#include <stdlib.h>  

FILE *stream;  

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

   errno_t err = fopen_s( &stream, "fscanf.out", "w+" );  
   if( err )  
      printf_s( "The file fscanf.out was not opened\n" );  
      fprintf_s( stream, "%s %ld %f%c", "a-string",   
               65000, 3.14159, 'x' );  
      // Set pointer to beginning of file:  
      fseek( stream, 0L, SEEK_SET );  

      // Read data back from file:  
      fscanf_s( stream, "%s", s, _countof(s) );  
      fscanf_s( stream, "%ld", &l );  

      fscanf_s( stream, "%f", &fp );  
      fscanf_s( stream, "%c", &c, 1 );  

      // Output data read:  
      printf( "%s\n", s );  
      printf( "%ld\n", l );  
      printf( "%f\n", fp );  
      printf( "%c\n", c );  

      fclose( stream );  

请参阅See Also

流 I/O Stream I/O
_cscanf_s、_cscanf_s_l、_cwscanf_s、_cwscanf_s_l _cscanf_s, _cscanf_s_l, _cwscanf_s, _cwscanf_s_l
fprintf_s、_fprintf_s_l、fwprintf_s、_fwprintf_s_l fprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l
scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_l scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
sscanf_s、_sscanf_s_l、swscanf_s、_swscanf_s_l sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l
fscanf、_fscanf_l、fwscanf、_fwscanf_lfscanf, _fscanf_l, fwscanf, _fwscanf_l