int  fscanf ( FILE * stream , const char * format [ , argument , ...] );

Read formatted data from a stream.
  Reads data from the current position of stream and stores it into the locations given by argument(s). Locations pointed by each argument are filled with their corresponding type of value requested in the format string.
  There must be the same number of type specifiers in format string than arguments passed.


Pointer to an open file.
String that can contain one or more of these items:
pointer to objects or structures to be filled with data read as specified by format string. There must be the same number of these parameters than the number of format tags. NOTE: These arguments must be pointers: if you want to store the result of a scanf operation on a standard variable you should precede it with the reference operator, i.e. an ampersand sign (&), like in:
int n;
fscanf (stream,"%d",&n);

Return Value.
  The number of items succesfully read. This count doesn't include any ignored fields.
  If EOF is returned an error has occurred before the first assignation could be done.

  Defined in ANSI-C.


/* fscanf example */
#include <stdio.h>

main ()
  char str [80];
  float f;
  FILE * pFile;

  pFile = fopen ("myfile.txt","w+");
  fprintf (pFile, "%f %s", 3.1416, "PI");
  rewind (pFile);
  fscanf (pFile, "%f", &f);
  fscanf (pFile, "%s", str);
  fclose (pFile);
  printf ("I have read: %f and %s \n",f,str);
  return 0;
This sample code creates a file called myfile.txt and stores a float number and a string, then, the stream is rewinded and both values are read with fscanf. Finally produces this output:
I have read: 3.141600 and PI

See also.
  scanf, fprintf, fopen, fclose

© The C++ Resources Network, 2000