3 Using FIO/RIO

 3.1 Routines to enhance simple FORTRAN I/O
 3.2 The stand-alone subroutines
 3.3 The environment level routines

FIO/RIO can be used in three main ways; you can use it in a minimalist way to ease the writing of normal FORTRAN programs, you can use the extra functionality provided by FIO file descriptors in stand alone FORTRAN programs, or you can use the ADAM parameter system interface in ADAM programs.

3.1 Routines to enhance simple FORTRAN I/O

Some of the FIO/RIO routines do not use FIO file descriptors and are provided to simplify common I/O operations. For example, FIO_GUNIT will get an unused FORTRAN unit number. Using this routine is better than ‘hard wiring’ unit numbers into code as you may not know what unit numbers other subroutines are using. The routines that do not use the FIO file descriptors are:

FIO_ERASE
Erase a file
FIO_GUNIT
Get a FORTRAN I/O unit number
FIO_PUNIT
Return an FORTRAN I/O unit number
FIO_REP
Report an I/O error
FIO_SERR
Report an I/O error
FIO_TEST
Test if a status value belongs to a certain class of errors
RIO_ERASE
Erase a file

Here is an example of the use of some of these routines.

        ...
  *  Get a unit number.
        CALL FIO_GUNIT( UNIT, STATUS )
  *  Open a file.
        OPEN( UNIT=UNIT, FILE=FILNAM, STATUS=’NEW’, IOSTAT=ISTAT )
        IF ( IOSTAT .EQ. 0 ) THEN
  *  Save the data.
           WRITE( UNIT, ’(5F10.2)’ ) ( X( I ), I = 1, 5 )
           CLOSE( UNIT )
        ELSE
  *  Report an error
           CALL FIO_REP( UNIT, FILNAM, ISTAT, ’ ’, STATUS )
        END IF
  *  Return the unit number.
        CALL FIO_PUNIT( UNIT, STATUS )
        ...

Consistent use of the FIO_GUNIT and FIO_PUNIT routines has reduced the likelihood of a clash of unit number between this part of the program and some other part, and the use of FIO_REP allows machine independent reporting of any errors.

3.2 The stand-alone subroutines

In addition to the routines in the previous section, FIO provides a set of routines to do some simple I/O on files. FIO maintains a set of file descriptors for active files which are used by these routines. These descriptors contain such things as the access mode of a file (read only, update, etc.), which allow FIO to trap some errors rather than permitting a run time error to occur. For example, if an attempt is made to write to a file that has been opened with ‘read only’ access, FIO will report the error, but the program will not crash, allowing the user to take corrective action. Use of these routines also makes user written code more portable. Issues such as requiring CARRIAGECONTROL=’LIST’ in DEC FORTRAN OPEN statements are handled internally. The routines that handle FIO file descriptors are:

FIO_CLOSE
Close a file.
FIO_FNAME
Get the name of a file.
FIO_OPEN
Open a file.
FIO_READ
Read a file.
FIO_READF
Read a file (faster than FIO_READ).
FIO_RWIND
Rewind a file.
FIO_UNIT
Get the unit number of a file.
FIO_WRITE
Write a file.
RIO_CLOSE
Close a file.
RIO_OPEN
Open a file.
RIO_READ
Read a file.
RIO_WRITE
Write a file.

Note that the same file descriptors are used by the FIO and RIO routines, so these can be freely mixed, where appropriate.

Here is an example of the use of some of these routines.

        ...
  *  Open a file.
        CALL FIO_OPEN( FILNAM, ’WRITE’, ’LIST’, 0, FD, STATUS )
  *  Write the data.
           DO I = 1, N
              CALL FIO_WRITE( FD, BUF( I ), STATUS )
           END DO
  *  Close the file.
        CALL FIO_CLOSE( FD, STATUS )
        ...

Note that there is no testing for errors in this piece of code since the FIO routines follow the normal Starlink convention for error handling and will not execute if STATUS is bad. However, if the loop is to be executed many times, it would be worth testing that the call to FIO_OPEN was successful, otherwise you could end up executing the loop many times to no effect.

3.3 The environment level routines

The last way of using FIO/RIO is in its fully integrated ADAM form. The following routines provide an interface to the ADAM parameter system:

FIO_ANNUL
Annul a file descriptor and close the file.
FIO_ASSOC
Open a file associated with an ADAM parameter.
FIO_CANCL
Close a file and cancel the parameter.
RIO_ANNUL
Annul a file descriptor and close the file.
RIO_ASSOC
Open a file associated with an ADAM parameter.
RIO_CANCL
Close a file and cancel the parameter.

These routines are typically used to get the name of a file through the ADAM parameter system. For instance, the previous example could be re-written as:

        ...
  *  Open a file.
        CALL FIO_ASSOC( PNAME, ’WRITE’, ’LIST’, 0, FD, STATUS )
  *  Write the data.
           DO I = 1, N
              CALL FIO_WRITE( FD, BUF( I ), STATUS )
           END DO
  *  Close the file.
        CALL FIO_CANCL( PNAME, STATUS )
        ...

When the call to FIO_ASSOC is executed, the name of the file will be obtained via the parameter system. This may involve prompting the user, but the file name could equally well be defaulted from the interface file. The interface file might contain something like this:

  PARAMETER  FILE
   TYPE     ’FILENAME’
   VPATH    ’PROMPT’
   PROMPT   ’Name of file to be created’
   PPATH    ’CURRENT,DEFAULT’
   DEFAULT  newfile.dat
  END PARAMETER

N.B. At present, if you specify a file name that contains a directory name in an interface file, then you must use the appropriate (Unix or VMS) syntax. In the future, FIO may be enhanced to handle environment variables and logical names as part of the file specification.