Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

162 StandardI/O Library Chapter 5


Refer to Section 7.3 of Kernighan and Ritchie[ 1988 ] for additional details on
handling variable-length argument lists with ISO StandardC.Beawarethat the
variable-length argument list routines provided with ISO C—the<stdarg.h>header
and its associated routines — differ from the<varargs.h>routines that wereprovided
with older UNIX systems.

Formatted Input


Formatted input is handled by the threescanffunctions.
#include <stdio.h>
int scanf(const char *restrict format,...);
int fscanf(FILE *restrict fp,const char *restrictformat,...);
int sscanf(const char *restrict buf,const char *restrictformat,...);
All three return: number of input items assigned,
EOFif input error or end of file beforeany conversion
Thescanffamily is used to parse an input string and convert character sequences into
variables of specified types. The arguments following the format contain the addresses
of the variables to initialize with the results of the conversions.
The format specification controls how the arguments areconverted for assignment.
The percent sign (%) indicates the beginning of a conversion specification. Except for
the conversion specifications and white space, other characters in the format have to
match the input. If a character doesn’t match, processing stops, leaving the remainder
of the input unread.
Thereare three optional components to a conversion specification, shown in square
brackets below:

%[*][fldwidth][m][lenmodifier]convtype

The optional leading asterisk is used to suppress conversion. Input is converted as
specified by the rest of the conversion specification, but the result is not stored in an
argument.
Thefldwidthcomponent specifies the maximum field width in characters. The
lenmodifiercomponent specifies the size of the argument to be initialized with the
result of the conversion. The same length modifiers supported by theprintffamily of
functions aresupported by thescanffamily of functions (see Figure5.8 for a list of the
length modifiers).
Theconvtypefield is similar to the conversion type field used by theprintf
family,but thereare some differences. One difference is that results that arestored in
unsigned types can optionally be signed on input. For example,−1will scan as
4294967295 into an unsigned integer.Figure5.10 summarizes the conversion types
supported by thescanffamily of functions.
The optionalmcharacter between the field width and the length modifier is called
theassignment-allocation character.Itcan be used with the%c,%s,and%[conversion
Free download pdf