O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using fscanf

In the same way that printf has a companion function scanf for handling keyboard input, fprintf has a companion function fscanf for handling formatted file input based on the conversion of input fields. The fscanf function reads and translates fields according to a specific format and assigns them to the parameter list from left to right.

The function definition is as follows:

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

The return value from fscanf is slightly different from fprintf, in that it contains the number of specific items read and converted, without counting any fields that have been ignored. Recall that fprintf returns the number of actual characters output to the file.

Also, if an error occurs, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required