fscanf

Description

Reads formatted data from a stream.

#include <stdio.h>

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

stream Pointer to FILE structure  
format Format-control string  
argument Optional arguments  

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 with a type that corresponds to a type specifier in format. The format controls the interpretation of the input fields and has the same form and function as the format argument for the scanf function; see scanf for a description of format.

Return Value

The fscanf function returns the number of fields that were successfully converted and assigned. The return value does not include fields that were read but not assigned.

The return value is EOF for an error or end-of-file on stream before the first conversion. A return value of 0 means that no fields were assigned.

Compatibility

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN

32-Bit:DOS32X

See Also

_cscanf, fprintf, scanf, sscanf

Example

/* 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;

void main( void )

{

long l;

float fp;

char s[81];

char c;

int result;

stream = fopen( “fscanf.out”, “w+” );

if( stream == NULL )

printf( “The file fscanf.out was not opened\n” );

else

{

fprintf( 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( 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 );

}

}

Output

a-string

65000

3.141590

x