fscanf, _fscanf_l, fwscanf, _fwscanf_l

Read formatted data from a stream. More secure versions of these functions are available; see 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 ]... 
);

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 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 and fwscanf.

These functions validate their parameters. If stream or format is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return EOF and set errno to EINVAL.

Remarks

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. 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 is a wide-character version of fscanf; the format argument to fwscanf is a wide-character string. These functions behave identically identically if the stream is opened in ANSI mode. fscanf doesn't currently support input from a UNICODE stream.

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 routine

_UNICODE & _MBCS not defined

_MBCS defined

_UNICODE defined

_ftscanf

fscanf

fscanf

fwscanf

_ftscanf_l

_fscanf_l

_fscanf_l

_fwscanf_l

For more information, see Format Specification Fields – scanf functions and wscanf Functions.

Requirements

Function

Required header

fscanf, _fscanf_l

<stdio.h>

fwscanf, _fwscanf_l

<stdio.h> or <wchar.h>

For additional compatibility information, see Compatibility in the Introduction.

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

.NET Framework Equivalent

System::IO::StreamReader::ReadLine. See also Parse methods, such as System::Double::Parse.

See Also

Reference

Stream 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