

#include <stdio.h> int fscanf(FILE *f, const char *format, loc1, loc2, ...);
fscanf reads formatted input from the FILE designated by f
according to the format specified by the string format. Following the
format in the argument list may be one or more additional pointers
(loc1, loc2,..., locn), addressing storage where the input values
are stored.
format points to a string that contains zero or more of the following:
The format string contains format specifiers or characters to be matched from the input. Format items have the following form:
%[*][ {OB} width {OBE} ][h | l | L]form
The specifiers have the following meanings:
width is present, width specifies the maximum width of the
input item.
h before a d, i, or n conversion specifier
indicates that the corresponding argument is a pointer to short int
instead of int.
l before a d, i, or n conversion specifier
indicates that the corresponding argument is a pointer to long int
instead of int.
h before an o, u, or x conversion specifier
indicates that the corresponding argument is a pointer to
unsigned short int instead of unsigned int.
l before an o, u, or x conversion specifier
indicates that the corresponding argument is a pointer to
unsigned long int instead of unsigned int.
l before an e, f, or g conversion specifier
indicates that the corresponding argument is a pointer to double instead
of float.
L before an e, f, or g conversion specifier
indicates that the corresponding argument is a pointer to long double
instead of float.
form is one of the following characters, defining the type of the
corresponding target object and the expected format of the input:
c
d
strtol with base=10. The
corresponding argument should beint *.
e, E, f, g, or G
float *.
i
int *.
n
fscanf is stored in the object
addressed by the corresponding int * argument.
o
unsigned int *.
p
%p printf format. This
implementation treats %p like %x. The corresponding argument
should be void **.
s
u
unsigned int *.
x, X
unsigned int *.
fscanf format.
The format string is a C string. With the exception of the c and
[ or < specifiers, white-space characters in the format string cause
white-space characters in the input to be skipped. Characters other than format
specifiers are expected to match the next nonwhite-space character in the
input. The input is scanned through white space to locate the next input item
in all cases except the c and [] specifiers, where the initial
scan is bypassed. The s specifier terminates on any white space.
The fscanf formats are described in more detail in the ISO/ANSI C standard.
As an extension, uppercase characters may also be used for the format characters
specified in lowercase in the previous list.
fscanf returns EOF if end of file (or an input error) occurs
before any values are stored. If values are stored, it returns the number of
items stored; that is, the number of times a value is assigned with one of the
fscanf argument pointers.
EOF is returned if an error occurs before any items are matched.
fscanf treats multibyte characters in the format string and in
conversions, see Chapter 11 in the SAS/C Library Reference, Volume 2 .
Because square brackets do not exist on some 370 I/O devices, the library allows
the format %[xyz] to be replaced by the alternate form %<xyz>.
This is not a portable format.
fscanf:
#include <stdio.h>
#include <stdlib.h>
static char *lines[] = {
"147.8 poundsn"
"51.7 milesn",
"4.3 light-yearsn",
"10000 voltsn",
"19.5 gallonsn"
};
main()
{
FILE *tmpf;
int i;
float amount;
char unit[20];
int count;
tmpf = tmpfile();
if (!tmpf){
puts("Couldn't open temporary file.");
exit(EXIT_FAILURE);
}
for (i = 0; i < sizeof(lines)/sizeof(char *); ++i){
fputs(lines[i], tmpf);
}
rewind(tmpf);
for(;;){
count = fscanf(tmpf, "%f %s", &amount, unit);
if (feof(tmpf)) break;
if (count < 2){
puts("Unexpected error in input data.");
exit(EXIT_FAILURE);
}
printf("amount = %f, units = "%s"n", amount, unit);
}
fclose(tmpf);
}
fprintf, scanf, sscanf
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.