fscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at fscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l.
Reads formatted data from a stream. These versions of fscanf, _fscanf_l, fwscanf, _fwscanf_l have security enhancements, as described in Security Features in the CRT.
Syntax
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 ]...
);
Parameters
stream
Pointer to FILE
structure.
format
Format-control string.
argument
Optional arguments.
locale
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. A return value of 0 indicates that no fields were assigned. If 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. 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. If execution is allowed to continue, these functions return EOF
and set errno
to EINVAL
.
Remarks
The fscanf_s
function reads data from the current position of stream
into the locations that are 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
. 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_s
is a wide-character version of fscanf_s
; the format argument to fwscanf_s
is a wide-character string. These functions behave identically if the stream is opened in ANSI mode. fscanf_s
doesn't currently support input from a UNICODE stream.
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. For more information, see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l and scanf Width Specification.
Note
The size parameter is of type unsigned
, not size_t
.
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 routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
---|---|---|---|
_ftscanf_s |
fscanf_s |
fscanf_s |
fwscanf_s |
_ftscanf_s_l |
_fscanf_s_l |
_fscanf_s_l |
_fwscanf_s_l |
Requirements
Function | Required header |
---|---|
fscanf_s , _fscanf_s_l |
<stdio.h> |
fwscanf_s , _fwscanf_s_l |
<stdio.h> or <wchar.h> |
For additional compatibility information, see Compatibility.
Example
// 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" );
else
{
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 );
}
}
a-string
65000
3.141590
x
.NET Framework Equivalent
System::IO::StreamReader::ReadLine. See also Parse
methods, such as System::Double::Parse.
See Also
Stream I/O
_cscanf_s, _cscanf_s_l, _cwscanf_s, _cwscanf_s_l
fprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l
scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l
fscanf, _fscanf_l, fwscanf, _fwscanf_l