HP Fortran Programmer's Reference (September 2007)

I/O and file handling
Example programs
Chapter 8 231
Phil Atley 9 27 35 49
Harriet Myrle 84 78 93 95 97 92 84 93
Pete Hartley 67 54 58 71 93 58
Here are the command lines to compile and execute the program, along with the output from
a sample run:
$ f90 nonadvance.f90
$ a.out
Name Average
--------------------------
Sandra Delford 86
Joan Arunsoelton 57
Herman Pritchard 68
Felicity Holmes 78
Anita Jayson 85
Phil Atley 30
Harriet Myrle 89
Pete Hartley 66
File access
The following example, file_access.f90, illustrates both sequential and direct access on
external files. The file opened for direct access is a scratch file. The comments explain what
the program does.
Example 8-4 file_access.f90
! This program uses an external file and a scratch file to
! insert a number into a list of numerically sorted numbers.
! The sorted list is held in a external file. The program uses
! the scratch file as a temporary holding place. The program
! uses direct access method with the scratch file.
PROGRAM main
REAL :: number_to_insert, number_in_list
INTEGER :: rec_num, ios1, ios2, i
! Initialize counter.
rec_num = 0
! ios1 must be initialized to 0 so that the error-handling
! section at the end of the program will work correctly
ios1= 0
! Open the scratch file and the sequential data file
OPEN (18, FILE='list', STATUS='UNKNOWN', IOSTAT=ios1, ERR=99)
OPEN (17, STATUS='SCRATCH', ACCESS='DIRECT', FORM='FORMATTED',
&