HP-UX Reference (11i v2 04/09) - 3 Library Functions N-Z (vol 7)

s
scanf(3S) scanf(3S)
[EILSEQ] The data obtained from the input stream does not form a valid wide character.
Additional
errno values can be set by the underlying
read() function (see read(2)).
EXAMPLES
The call:
int i, n; float x; char name[50];
n = scanf("%d%f%s", &i, &x, name);
with the input line:
25 54.32E-1 thompson
assigns to n the value 3,toi the value
25,tox the value 5.432, and name contains thompson\0. Or:
int i; float x; char name[50];
(void) scanf("%2d%f%*d %[0-9]", &i, &x, name);
with input:
56789 0123 56a72
assigns 56 to i, 789.0 to x, skips
0123, and places the string 56\0 in name. The next call to
getchar() (see getc(3S)) returns a.
For another example, to create a language-independent date scanning routine, use:
char month[20]; int day, year;
(void) scanf(format, month, &day, &year);
For American usage, format would point to a string:
%1$s %2$d %3$d
The input:
July 3 1986
would assign July to month, 3 to day and 1986 to year.
For German usage, format would point to a string:
%2$d %1$s %3$d
The input:
3 Juli 1986
would assign
Juli to month, 3 to day and 1986 to year.
The success of literal matches and suppressed assignments can be determined with the
%n conversion
specification. Here is an example that checks the success of literal matches:
int i, n1, n2, n3, n4;
n1=n2=n3=n4=-1;
scanf("%nBEGIN%n %d %nEND%n", &n1, &n2, &i, &n3, &n4);
if (n2 - n1 == 5) puts( "matched BEGIN");
if (n4 - n3 == 3) puts( "matched END");
Here is an example that checks the success of suppressed assignments:
int i, n1, n2;
n1=n2=-1;
scanf( "%d %n%*s%n", &i, &n1, &n2);
if (n2 > n1)
printf("successful assignment suppression of %d chars\n", n2-n1);
APPLICATION USAGE
After scanf() or fscanf() is applied to a stream, the stream becomes byte-oriented (see orienta-
tion(5)).
HP-UX 11i Version 2: September 2004 4 Hewlett-Packard Company Section 3945