HP C/iX Library Reference Manual (30026-90004)
Chapter 2 15
HP C/iX Library Input and Output
HP C/iX Library Input and Output
After a stream has been opened, you may read from it or write to it in several ways.
Reading or writing can be done on a character-by-character basis using the inline macros
getc and putc, or on a block-by-block basis using functions such as fread or fwrite. The
macros getchar and putchar and the higher-level functions fgetc, fgets, fprintf,
fputc, fputs, fread, fscanf, fwrite, gets, printf, puts, and scanf use, or act as if they
use, getc and putc; they may be freely intermixed.
An open stream can also be controlled by functions such as fseek and rewind. These
functions allow you to position the stream position indicator to an arbitrary byte.
When you are finished using a stream, the stream should be closed. This may be
accomplished by issuing a call to fclose, implicitly by calling exit, or by returning from
the main function. It is important to close all files because the fclose function causes
information that is buffered in memory to be written out to the physical file. Calling an
MPE termination routine does not properly close open streams.
Stream Types
The HP C/iX library supports two stream types: text and binary streams. The stream type
dictates how special characters are to be processed and how records are to be padded.
Streams created by the HP C/iX library default to text streams.
Text Streams
A text stream is an ordered sequence of characters composed into lines with each line
consisting of zero or more characters plus a terminating newline (\n) character.
On MPE/iX, text streams are line-oriented fixed record length ASCII files. Text streams
are usually the product of editor programs and are read directly without any
interpretation by other functions. The newline character is not actually written to the file,
but is used by the HP C/iX I/O functions to indicate when a buffer is full of information and
should be posted to the file.
On input, newline characters are added to the records read from the file to make it appear
as if the newline character is actually in the file. This is done to allow programs, such as
EDITOR, to produce files that can be read by C functions in a manner compatible with other
systems. ASCII files managed by MPE do not actually contain \n characters, but appear to
when read by C functions. Further, the type of record structure used in the file has impact
on what is seen when a file is read. Assume that there is an ASCII MPE file with
variable-length records. If the following 5 bytes are written:
'A', 'B', 'C', 'D', '\n'
only 4 bytes are actually output to the MPE file. The record in the file appears as:
ABCD
The \n is used by the C functions to indicate that a record should be written, but the
newline character is not actually written. When the same record is read back in, the \n is
added to the end of the buffer. The result is that successive getc operations return the same
five characters originally written out:
'A', 'B', 'C', 'D', '\n'
If the same example is examined with an ASCII file composed of fixed-length records, each