Scorpio News |
October–December 1988 – Volume 2. Issue 4. |
| Page 16 of 35 |
|---|
The C I/O system starts with a set of character level functions: getc(chan),
putc(chan,
Built on the basic I/O above are string I/O functions: puts(str), gets(str),
fputs(str,
The most useful functions for high level I/O are printf() and scanf(). There are also
file based equivalents fprintf() and fscanf(), and string based versions sprintf()
and sscanf(). These both take a variable number of arguments (they are variadic),
the first of which is a control string (with the string and file versions the control string
comes second after the file-pointer or string-pointer). The control string declares
how the output/
printf ( "File %s, %d records in %dK\n", filename, record_count, size );
Which would give, for a file FRED with 25 records in 4K, the output
File FRED, 25 records in 4K <newline>
The item declarators following % symbols can specify minimum and maximum field widths, left or right justify, and in the case of numeric values the leading zeros can optionally be displayed. This is best shown by a table:
| Control string | Values | Result |
| "%c' – %d <%x>" | 48,48,48 | '0' – 48 <30> |
| "*%−15s %02d*" | "Total",5 | *Total 05* |
| "%3d %3d %c" | 12,6,83 | 12 6 S |
The scanf() function attempts to match the input to the control and assigns values accordingly. For example, if you expect an input like “15 : x (4,5)”, you might use a control such as " %d : %c ( %d, %d )". The spaces in the control will match any space (or no space) on the input.
| Page 16 of 35 |
|---|