This documentation is archived and is not being maintained.

fscanf, fwscanf

Read formatted data from a stream.

int fscanf( 
   FILE *stream,
   const char *format [,
   argument ]... 
int fwscanf( 
   FILE *stream,
   const wchar_t *format [,
   argument ]... 


Pointer to FILE structure.
Format-control string.
Optional arguments.

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.


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. If copying takes place between strings that overlap, the behavior is undefined.

fwscanf is a wide-character version of fscanf; the format argument to fwscanf is a wide-character string. These functions behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_ftscanf fscanf fscanf fwscanf

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


Function Required header Compatibility
fscanf <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
fwscanf <stdio.h> or <wchar.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


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

FILE *stream;

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

   stream = fopen( "fscanf.out", "w+" );
   if( stream == NULL )
      printf( "The file fscanf.out was not opened\n" );
      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 );
      fscanf( stream, "%ld", &l );

      fscanf( stream, "%f", &fp );
      fscanf( stream, "%c", &c );

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

      fclose( stream );



See Also

Stream I/O Routines | _cscanf | fprintf | scanf | sscanf | Run-Time Routines and .NET Framework Equivalents