HP 3000 Manuals

Internal Files [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Programmer's Guide

Internal Files 

Internal files provide a way of reformatting, converting, and
transferring data from one area of memory to another; no input or output
devices are used.  If you use internal files to reformat data, you do not
have to write data to a file and reread the file with a different format.
Instead, internal files allow conversion between numeric and character
data types.

An internal file is an area of storage internal to the program.  An
internal file can be a character variable, a character array element, a
character array, or a character substring.  Each variable, substring, or
array element is one record; if the file is an array, each array element
is one record.  For example, the statement:

     CHARACTER*15 city(50)

defines a character array containing 50 elements of 15 characters each.
The array can be referenced as an internal file named city containing 50
records of 15 characters each.

You cannot use the OPEN or CLOSE statement, any file positioning
statements, or any file status statements with internal files.

Reading From an Internal File 

Data is usually read from internal files with a formatted READ statement.
The name of the internal file is specified on the READ statement.  For
example, the READ statement in the following program reads four records
from the internal file course and stores the data into variables a, b, c,
and d.

           PROGRAM internal_read1

           CHARACTER*10 course(4), a, b, c, d
           DATA course / 'chemistry','biology','zoology','botany'/

     C     Read from internal file 'course'
           READ(course, '(A10)') a, b, c, d

           WRITE(6,*) a
           WRITE(6,*) b
           WRITE(6,*) c
           WRITE(6,*) d

           STOP
           END

The output of this program is:

     chemistry
     biology
     zoology
     botany

You can use the READ statement to convert data.  For example, consider
the following program:

           PROGRAM internal_read2

           CHARACTER  int_file*20, string*20
           INTEGER    a, b, c, d, e
           DATA       string /'  31  61   4  18  91'/

     C  Assign the character string to the internal file:
           int_file = string

     C  Read the internal file and convert the data type:
           READ(int_file, '(5I4)') a, b, c, d, e

     C  Check the contents of the file:
           WRITE(6,*) 'a = ', a
           WRITE(6,*) 'b = ', b
           WRITE(6,*) 'c = ', c
           WRITE(6,*) 'd = ', d
           WRITE(6,*) 'e = ', e

           END

This program performs the following:  reads the character data from the
internal file int_file, converts the character string into internal
integer format, and stores the converted data into the variables a, b, c,
d, and e.  The output of the program is shown below:

     a = 31
     b = 61
     c = 4
     d = 18
     e = 91

As an HP extension to the FORTRAN 77 standard, list-directed READ
operations can use internal files.

Writing to an Internal File 

Data is written to internal files with a formatted WRITE statement.  The
name of the internal file is specified in the WRITE statement.  For
example, the statement:

     WRITE(UNIT=address_var, FMT='(I10)') street_address

or:

     WRITE(address_var, '(I10)') street_address

writes the value of street_address into the first ten positions of the
internal file address_var.  The variable address_var must be a variable
or array of type CHARACTER. If address_var has a length greater than ten,
the rest of the record is filled with blanks.

The WRITE statement in the program below defines an internal file name
and writes five records to the file.

           PROGRAM internal_write

           CHARACTER*14 name(5)
           INTEGER      a, b, c, d, e
           DATA         a/14/, b/57/, c/0/, d/-123/, e/95/

     C  Write to the internal file:

           WRITE(name, '(1X, I4)') a, b, c, d, e

           END

After the program executes, the array name is assigned the following
values (the values are 14 characters long):

---------------------------------------------
|              |                            |
|   Element    |           Value            |
|              |                            |
---------------------------------------------
|              |                            |
|   name(1)    |       14                   |
|              |                            |
|   name(2)    |       57                   |
|              |                            |
|   name(3)    |        0                   |
|              |                            |
|   name(4)    |   -123                     |
|              |                            |
|   name(5)    |      95                    |
|              |                            |
-              -                            -
|              | <------14 characters long------>
|              |                            |
---------------------------------------------

Another example of writing to a character variable in an internal file is
shown below, where a format specification is built at execution time.
This program only shows how internal files work, not necessarily
efficient programming practices.

           PROGRAM internal_write2

           CHARACTER*14  ifmt
           INTEGER       iarray(5)
           DATA          iarray/1, 2, 3, 4, 5/

     C  Prompt for format specification variables:
           WRITE(6,*) 'Enter number of spaces before the array contents:'
           READ(5,*) n
           WRITE(6,*) 'Enter the repetition factor: '
           READ(5,*) m

     C  Create the internal file containing the format:
           WRITE(ifmt,10) n, m
      10   FORMAT (1X, '(', I2, 'X,' I2, '(I2,X))')

     C  Print the array using the format in the internal file:
           WRITE(6,*) 'The contents of the array "iarray" is: '
           WRITE(6,ifmt) iarray

     C  Print the format used to store the data:
           WRITE(6,*) 'The format used to print the contents is: '
           WRITE(6,*) 'FORMAT ', ifmt

           END

The program prompts you for portions of the FORMAT statement; the WRITE
statement then writes the variables n and m to the character string in
the internal file.  Therefore, when the WRITE statement executes, the
format specification will have the desired format.

A sample run of the program looks like this:

     Enter number of spaces before the array contents: 10 
     Enter the repetition factor: 5 
     The contents of the array "iarray" is: 1 2 3 4 5 
     The format used to print the contents is: FORMAT (10X, 5(I2,X)) 

As an HP extension to the FORTRAN 77 standard, list-directed WRITE
operations can use internal files.



MPE/iX 5.0 Documentation