02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

I/O and file handling<br />

Example programs<br />

! until it encounters end-of-record. When the<br />

! program encounters end-of-record, it starts a new record.<br />

! When it encounters and end-of-file,<br />

! the program is done. For the sake of simplicity, the<br />

! program does no error-checking.<br />

PROGRAM main<br />

INTEGER :: grade, count, sum, average<br />

CHARACTER(LEN=20) name<br />

OPEN(20, FILE='grades')<br />

WRITE (6, 10) ”Name”, ”Average”<br />

WRITE (6, *) ”--------------------------”<br />

DO ! read and process each record<br />

sum = 0<br />

count = 0<br />

! Read the first field of each record, using nonadvancing<br />

! I/O so as not to advance beyond that field. The END=<br />

! specifier causes the program to exit the loop and branch<br />

! to the statement at 999 when it detects end-of-file.<br />

READ(20, ”(A20)”, ADVANCE='NO', END=999) name<br />

196<br />

! Read each of the score fields of the record, using<br />

! nonadvancing I/O to avoid advancing to the next record<br />

! after each read. The EOR= specifier causes the program<br />

! to break out of the loop and resume<br />

! execution at the statement labeled 99.<br />

DO ! inner loop to read scores<br />

! read a score and convert it to integer<br />

READ(20, ”(I3)”, ADVANCE='NO', EOR=99) grade<br />

count = count + 1<br />

sum = sum + grade<br />

END DO<br />

! calculate average<br />

99 average = sum/count<br />

WRITE(6, 20) name, average ! write student name and average<br />

END DO<br />

10 FORMAT (X, A, T21, A)<br />

20 FORMAT (X, A, I3)<br />

999 CLOSE(20)<br />

END PROGRAM main<br />

Example 8-3 grades<br />

Sandra Delford 79 85 81 72100100<br />

Joan Arunsoelton 8 64 77 79<br />

Herman Pritchard 100 92 87 65 0<br />

Felicity Holmes 97 78 58 75 88 73<br />

Anita Jayson 93 85 90 95 68 72 93<br />

Chapter 8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!