HP C/iX Library Reference Manual (30026-90004)

180 Chapter5
HP C/iX Library Function Descriptions
fscanf
% Matches a single %. No conversion or assignment occurs. The complete
conversion specification is &%&%
Integer Conversion Characters
The d, o, and x conversion characters read characters from the stream until an
inappropriate character is encountered, or until the number of characters specified by the
field width
, if given, is exhausted (whichever comes first).
For d, an inappropriate character is any character except +, -, and 0 through 9. For o, an
inappropriate character is any character except +, -, and 0 through 7. For x, an
inappropriate character is any character except +, -, 0 through 9, and the characters a
through f and A through F. Note that negative octal and hexadecimal values are stored in
their twos complement form with sign extension. Thus, they might look unfamiliar if you
print them out later using printf().
These integer conversion characters can be preceded by a l to indicate that a long int
should be expected rather than an int. They can also be preceded by h to indicate a short
int. The corresponding items in the item list for these conversion characters must be
pointers to integer variables of the appropriate length.
Character Conversion Characters
The c conversion character reads the next character from the open stream no matter what
that character is. The corresponding item in the item list must be a pointer to a character
variable. If a
field width
is specified, the number of characters indicated by the
field
width
are read. In this case, the corresponding item must refer to a character array large
enough to hold the characters read.
Note that strings read using the c conversion character are not automatically terminated
with a null character in the array. Because all C library functions that use strings assume
the existence of a null terminator, be sure to add the '\0' character yourself. If you do not,
library functions are not able to tell where the string ends, and you get unexpected results.
The s conversion character reads a character string from the open stream, which is
delimited by one or more space characters (blanks, tabs, or newlines). If
field width
is
not given, the input string consists of all characters from the first nonspace character up to
(but not including) the first space character. Any initial space characters are skipped over.
If a
field width
is given, characters are read, beginning with the first nonspace
character, up to the first space character, or until the number of characters specified by the
field width
is reached (whichever comes first). The corresponding item in the item list
must refer to a character array large enough to hold the characters read, plus a
terminating null character, which is added automatically.
The s conversion character cannot be made to read a space character as part of a string.
Space characters are always skipped over at the beginning of a string, and they terminate
reading whenever they occur in the string. For example, suppose you want to read the first
character from the following input line:
" Hello, there!"
(Ten spaces followed by "Hello, there!"; the double quotes are added for clarity). If you use
%c, you get a space character. However, if you use %1s, you get "H" (the first nonspace