02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Phil Atley 9 27 35 49<br />

Harriet Myrle 84 78 93 95 97 92 84 93<br />

Pete Hartley 67 54 58 71 93 58<br />

I/O and file handling<br />

Example programs<br />

Here are the command lines to compile and execute the program, along with the output from<br />

asamplerun:<br />

$ f90 nonadvance.f90<br />

$ a.out<br />

Name Average<br />

--------------------------<br />

Sandra Delford 86<br />

Joan Arunsoelton 57<br />

Herman Pritchard 68<br />

Felicity Holmes 78<br />

Anita Jayson 85<br />

Phil Atley 30<br />

Harriet Myrle 89<br />

Pete Hartley 66<br />

File access<br />

The following example, file_access.f90, illustrates both sequential and direct access on<br />

external files. The file opened for direct access is a scratch file. The comments explain what<br />

the program does.<br />

Example 8-4 file_access.f90<br />

! This program uses an external file and a scratch file to<br />

! insert a number into a list of numerically sorted numbers.<br />

! The sorted list is held in a external file. The program uses<br />

! the scratch file as a temporary holding place. The program<br />

! uses direct access method with the scratch file.<br />

PROGRAM main<br />

REAL :: number_to_insert, number_in_list<br />

INTEGER :: rec_num, ios1, ios2, i<br />

! Initialize counter.<br />

rec_num = 0<br />

! ios1 must be initialized to 0 so that the error-handling<br />

! section at the end of the program will work correctly<br />

ios1= 0<br />

! Open the scratch file and the sequential data file<br />

OPEN (18, FILE='list', STATUS='UNKNOWN', IOSTAT=ios1, ERR=99)<br />

OPEN (17, STATUS='SCRATCH', ACCESS='DIRECT', FORM='FORMATTED',<br />

&<br />

Chapter 8 197

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

Saved successfully!

Ooh no, something went wrong!