HP Fortran Programmer's Guide (B3908-90031; September 2011)

Writing HP-UX applications
Using HP-UX file I/O
Chapter 7178
Using HP-UX file I/O
HP-UX file-processing routines can be used as an alternative to Fortran file I/O routines. This section
discusses HP-UX stream I/O routines and I/O system calls.
Stream I/O using FSTREAM
The HP-UX operating system uses the term stream to refer to a file as a contiguous set of bytes. There are a
number of HP-UX subroutines for performing stream I/O; see stdio(3S) in the HP-UX Reference.
Unlike Fortran I/O, which requires a logical unit number to access a file, stream I/O routines require a
stream pointer—an integer variable that contains the address of a C-language structure of type FILE (as
defined in the C-language header file /usr/include/stdio.h.)
The following Fortran statement declares a variable for use as a stream pointer in HP Fortran:
INTEGER(4) :: stream_ptr
To obtain a stream pointer, use the Fortran intrinsic FSTREAM, which returns a stream pointer for an open
file, given the file's Fortran logical unit number:
stream-ptr = FSTREAM(logical-unit)
The logical-unit parameter must be the logical unit number obtained from opening a Fortran file, and
stream-ptr must be of type integer. If stream-ptr is not of type integer, type conversion takes place
with unpredictable results. The stream-ptr should never be manipulated as an integer.
Once you obtain stream-ptr, use the ALIAS directive to pass it by value to stream I/O routines. (For an
example of how to use the ALIAS directive, see “File handling” on page 198.) All HP Fortran directives are
described in the HP Fortran Programmer's Reference.)
Performing I/O using HP-UX system calls
File I/O can also be performed with HP-UX system calls (for example, open, read, write, and close),
which provide low-level access to the HP-UX kernel. These routines are discussed in the HP-UX Reference;
see also the online man pages for these routines. For an example program that shows how to call the write
routine, see “File handling” on page 198.