HP Fortran Programmer's Reference (September 2007)

I/O and file handling
Example programs
Chapter 8230
! until it encounters end-of-record. When the
! program encounters end-of-record, it starts a new record.
! When it encounters and end-of-file,
! the program is done. For the sake of simplicity, the
! program does no error-checking.
PROGRAM main
INTEGER :: grade, count, sum, average
CHARACTER(LEN=20) name
OPEN(20, FILE='grades')
WRITE (6, 10) ”Name”, ”Average”
WRITE (6, *) ”--------------------------”
DO ! read and process each record
sum = 0
count = 0
! Read the first field of each record, using nonadvancing
! I/O so as not to advance beyond that field. The END=
! specifier causes the program to exit the loop and branch
! to the statement at 999 when it detects end-of-file.
READ(20, ”(A20)”, ADVANCE='NO', END=999) name
! Read each of the score fields of the record, using
! nonadvancing I/O to avoid advancing to the next record
! after each read. The EOR= specifier causes the program
! to break out of the loop and resume
! execution at the statement labeled 99.
DO ! inner loop to read scores
! read a score and convert it to integer
READ(20, ”(I3)”, ADVANCE='NO', EOR=99) grade
count = count + 1
sum = sum + grade
END DO
! calculate average
99 average = sum/count
WRITE(6, 20) name, average ! write student name and average
END DO
10 FORMAT (X, A, T21, A)
20 FORMAT (X, A, I3)
999 CLOSE(20)
END PROGRAM main
Example 8-3 grades
Sandra Delford 79 85 81 72100100
Joan Arunsoelton 8 64 77 79
Herman Pritchard 100 92 87 65 0
Felicity Holmes 97 78 58 75 88 73
Anita Jayson 93 85 90 95 68 72 93