fscanf(3C)
_________________________________________________________________
fscanf function
Perform a formatted read from a file.
_________________________________________________________________
Calling Sequence
#include <stdio.h>
FILE *fp;
int fscanf();
char *format;
fscanf(fp, format, arg ... );
where fp is a pointer to the input FILE stream.
format is a string containing three kinds of
objects:
Whitespace characters: space, New Line, tab, and
carriage return, which cause fscanf to read input up
to the next non-whitespace character.
Ordinary, printable characters.
Conversion specifications that start with %.
Each arg is a valid C variable or constant.
Description
The fscanf function takes the format string and matches the input
to it. It attempts to read successive characters from a file and
convert the characters according to the specified format,
updating the variables that the successive arguments point to.
Both whitespace characters (those that take up space but do not
print) and ordinary printable characters are read.
For details of conversion specifications, see the scanf function.
Returns
The return value from fscanf is the number of items successfully
matched and assigned, or -1 if the function finds an end of file
or error and no characters were read.
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
fscanf(3C)
Related Functions
See also the scanf, sscanf, and fprintf functions.
Example
/* Program test for the fscanf() function */
#include <stdio.h>
#define MAXCLASS 5
#define MAXNAME 16
FILE *fp, *fopen();
char name[MAXNAME];
int i = 1, grade, fscanf();
main(argc, argv)
int argc;
char *argv[];
{
fp = fopen(argv[1], "r");
printf("Class: %s\n\n", argv[1]);
printf("NAME\t\tGRADE\n\n");
while (i <= MAXCLASS) {
fscanf(fp, "%s %d", &name, &grade);
printf("%s\t\t%d\n", name, grade);
i++;
}
}
A call to the program test with the valid filename Latin.1, which
contains
Brown 75
Jones 86
Smith 12
Varro 92
Vergil 57
generates the output
Class: Latin.1
NAME GRADE
Brown 75
Jones 86
Smith 12
Varro 92
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)
fscanf(3C)
Vergil 57
DG/UX 4.00 Page 3
Licensed material--property of copyright holder(s)