HP Fortran Programmer's Reference (September 2007)

I/O and file handling
Example programs
Chapter 8 229
REAL (KIND=8) :: x
CHARACTER (LEN=14) :: dp_val
CHARACTER :: dec_digits
! Use an edit descriptor to convert the value of n to a
! character; write the result to the internal file
! dec_digits.
WRITE (dec_digits, '(I1)') n
! Concatenate dec_digits to the string 'F14.'. The complete
! string forms an edit descriptor that will convert the
! binary value of x to a formatted value of x to a
! formatted character string that formats the
! value. The character represents the requested level of
! precision. The formatted number is stored in the internal
! file dp_val.
WRITE (dp_val, '(F14.'//dec_digits//')') x
! Re-convert the formatted record in dp_val to a binary
! value that the function will return.
READ (dp_val, '(F14.0)') roundoff
END FUNCTION roundoff
Here are the command lines to compile and execute the program, along with the output from
a sample run:
$ f90 int_file.f90
$ a.out
Enter a real number: 3.1415927
How many significant digits (1 - 9) to the
right of the decimal point? 3
3.142
Nonadvancing I/O
The following program reads a formatted sequential file as a set of records divided into an
arbitrary number of fields. The program uses nonadvancing I/O to read and process each field.
The comments explain what the program does. Included with the is a listing of the data file,
grades, read by the program.
Example 8-2 nonadvance.f90
! This program uses nonadvancing I/O to read a series of
! sequential-file records, character by character. Each
! record is divided into fields. The first field is the name
! of a student and is 20 characters log. Each of the
! remaining fields s a numeric test score and is 3
! i characters long. The name score fields. The program
! reads the name field, then reads each score field