[Previous] [Contents] [Next]

fscanf()

Scan input from a stream

Synopsis:

#include <stdio.h>

int fscanf( FILE* fp, 
            const char* format, 
            ... );

Library:

libc

Description:

The fscanf() function scans input from the stream specified by fp, under control of the argument format. Following the format string is a list of addresses to receive values. The format string is described under the description of the scanf() function.

Returns:

EOF when the scanning is stopped by reaching the end of the input stream before storing any values, or the number of input arguments for which values were successfully scanned and stored.

Errors:

If an error occurs, errno will be set to indicate the type of error.

Examples:

To scan a date in the form "Friday March 26 1999":

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    int day;
    int year;
    char weekday[10];
    char month[10];
    FILE *in_data;

    in_data = fopen( "file", "r" );
    if( in_data != NULL ) {
        fscanf( in_data, "%s %s %d %d",
                weekday, month, &day, &year );

        printf( "Weekday=%s Month=%s Day=%d Year=%d\n",
                weekday, month, day, year );

        fclose( in_data );
        
        return EXIT_SUCCESS;
    }
    
    return EXIT_FAILURE;
}

Classification:

ANSI

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes

See also:

errno, scanf(), sscanf(), vfscanf(), vsscanf()


[Previous] [Contents] [Next]