Scor­pio 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,​char), getchar() and putchar(char). The last two use the default streams. Unlike CP/M, UNIX uses Newline (ASCII 10) to separate lines rather than CR,​LF (13,​10). Also unlike CP/M, UNIX knows the exact length of all files, so end of file is reported by a value of −1 (which is why getc() and getchar() return an integer rather than a character). Because of these differences, files must be declared as either text or binary type. This is not a problem since UNIX versions of C must be able to cope with standard terminal devices so the facility is available.

Built on the basic I/O above are string I/O functions: puts(str), gets(str), fputs(str,​chan) and fgets(str,​len,​chan), and also word I/O getw(chan) and putw(word,​chan) and block I/O which accounts for a number of similar functions which I don’t have room to go into here. The behaviour of the string I/O is slightly different with the file functions. Specifically, puts() adds a newline, fputs() does not. Also fgets() has a length limit and leaves the newline in, whereas gets() overwrites the newline with the NULL.

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/​input should look. For example:

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 stringValuesResult
"%c' – %d <%x>"48,48,48'0' – 48 <30>
"*%−15s %02d*""Total",5*Total           05*
"%3d %3d %c"12,6,8312   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