Syntax
#include <stdio.h> int fscanf (FILE *stream, const char *format-string, argument-list);Description
fscanf reads data from the current position of the specified stream into the locations given by the entries in argument-list, if any. Each entry in argument-list must be a pointer to a variable with a type that corresponds to a type specifier in format-string.
The format-string controls the interpretation of the input fields and has the same form and function as the format-string argument for the scanf function. See scanf for a description of format-string.
In extended mode, the fscanf function also reads in the strings "INFINITY", "INF", and "NAN" (in uppercase or lowercase) and converts them to the corresponding floating-point value. The sign of the value is determined by the format specification. See Infinity and NaN Support for more information on infinity and NaN values.
fscanf returns the number of fields that it successfully converted and assigned. The return value does not include fields that fscanf read but did not assign.
The return value is EOF if an input failure occurs before any conversion, or the number of input items assigned if successful.
This example opens the file myfile.dat for reading and then scans this file for a string, a long integer value, a character, and a floating-point value.
#include <stdio.h> #define MAX_LEN 80 int main(void) { FILE *stream; long l; float fp; char s[MAX_LEN+1]; char c; stream = fopen("myfile.dat", "r"); /* Put in various data. */ fscanf(stream, "%s", &s[0]); fscanf(stream, "%ld", &l); fscanf(stream, "%c", &c); fscanf(stream, "%f", &fp); printf("string = %s\n", s); printf("long %ld\n", l); printf("char = %c\n", c); printf("float = %f\n", fp); return 0; /**************************************************************************** If myfile.dat contains: abcdefghijklmnopqrstuvwxyz 343.2. The output should be: string = abcdefghijklmnopqrstuvwxyz long double = 343 char = . float = 2.000000 ****************************************************************************/ }Related Information