HP C/iX Library Reference Manual (30026-90004)

14 Chapter2
HP C/iX Library Input and Output
HP C/iX Library Input and Output
HP C/iX Library Input and Output
The C language does not provide any direct facility to perform input or output. Instead, C
implementations usually provide a set of functions that perform I/O. Care must be used
when calling the I/O functions because the compiler does not ensure that the arguments to
the functions are correct. Most errors in calls to I/O functions may not provide the correct
results and may cause addressing violations and abnormal terminations.
HP C/iX provides two I/O facilities. You can call MPE intrinsics directly or call a set of
supplied C functions that provide an interface that is transportable to other systems.
Because most of the C functions are built on top of MPE, they may not execute as quickly
as direct MPE calls. The added time in most cases is small and the resulting portability is
usually well worth the extra execution time.
You should use either HP C/iX I/O functions or MPE/iX intrinsics, but not both, in the
same program. Using HP C/iX I/O functions with MPE/iX intrinsics to operate on the same
file can result in unpredictable program behavior. The HP C/iX I/O routines use data and
file control buffers that are different from the ones used by the MPE/iX I/O intrinsics.
HP C/iX I/O and POSIX I/O functions utilize a byte stream model. The data is treated as
a continuous stream of bytes. There are conceptually no record boundaries.
The HP C/iX I/O functions allow C programs to access any file type supported by MPE.
These functions emulate stream I/O when accessing MPE (non-byte stream) files.
The performance of stream emulation suffers when accessing variable-length records with
certain I/O functions. For example, the HP C I/O function fseek enables the caller to
position a file pointer to any given byte number. In the case of files that are composed of
fixed-sized records, the positioning operation is direct because the address of the record
that contains the interesting byte can be calculated and the file pointer can be positioned
to it. If a file has variable-length records, the fseek function still works, but the
implementation requires the file to be rewound and then all records are read until the
required record is reached. The fseek function is much slower with variable-length record
MPE files than with fixed-length record MPE files.
Basic Stream Usage
Using a stream is similar to using an MPE file. A stream is opened by calling the C library
function fopen(). The fopen function creates a data structure that contains descriptive
data about a stream and returns a pointer to this structure. This pointer designates the
stream in all further transactions.
When you use the fopen function to open an existing file, the fixed attributes of the file
take precedence over the mode requested by fopen(). In particular, an existing ASCII file
is opened as a text stream, and an existing binary file is opened as a binary stream,
regardless of the mode requested by fopen().
Three constant pointers that designate standard streams opened automatically by the C
startup routines can also be used in further transactions. Refer to the section called
"Standard Files" for details.