Monday, December 7, 2009

Format specification during input

The format string is used to control interpretation of a stream of input data, which generally contains values to be assigned to the objects pointed to by the remaining arguments to scanf. The contents of the format string may contain:

white space
This causes the input stream to be read up to the next non-white-space character.
ordinary character
Anything except white-space or % characters. The next character in the input stream must match this character.
conversion specification
This is a % character, followed by an optional * character (which suppresses the conversion), followed by an optional nonzero decimal integer specifying the maximum field width, an optional h, l or L to control the length of the conversion and finally a non-optional conversion specifier. Note that use of h, l, or L will affect the type of pointer which must be used.
Except for the specifiers c, n and [, a field of input is a sequence of non-space characters starting at the first non-space character in the input. It terminates at the first conflicting character or when the input field width is reached.


The result is put into wherever the corresponding argument points, unless the assignment is suppressed using the * mentioned already. The following conversion specifiers may be used:
d i o u x
Convert a signed integer, a signed integer in a form acceptable to strtol, an octal integer, an unsigned integer and a hexadecimal integer respectively.
e f g
Convert a float (not a double).
s
Read a string, and add a null at the end. The string is terminated by whitespace on input (which is not read as part of the string).
[
Read a string. A list of characters, called the scan set follows the [. A ] delimits the list. Characters are read until (but not including) the first character which is not in the scan set. If the first character in the list is a circumflex ^, then the scan set includes any character not in the list. If the initial sequence is [^] or [], the ] is not a delimiter, but part of the list and another ] will be needed to end the list. If there is a minus sign (-) in the list, it must be either the first or the last character; otherwise the meaning is implementation defined.
c
Read a single character; white space is significant here. To read the first non-white space character, use %1s. A field width indicates that an array of characters is to be read.
p
Read a (void *) pointer previously written out using the %p of one of the printfs.
%
A % is expected in the input, no assignment is made.
n
Return as an integer the number of characters read by this call so far.
The size specifiers have the effect shown in Table 9.7.
Specifier Modifies Converts
l d i o u x long int
h d i o u x short int
l e f double
L e f long double
Table 9.7. Size specifiers
The functions are described below, with the following declarations:
#include 

int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *s, const char *format, ...);
int scanf(const char *format, ...);
Fscanf takes its input from the designated stream, scanf is identical to fscanf with a first argument of stdin, and sscanf takes its input from the designated character array.
If an input failure occurs before any conversion, EOF is returned. Otherwise, the number of successful conversions is returned: this may be zero if no conversions are performed.
An input failure is caused by reading EOF or reaching the end of the input string (as appropriate). A conversion failure is caused by a failure to match the proper pattern for a particular conversion.

0 comments:

Post a Comment