Simple Fortran File Referencing

OBJECTIVES:

This document is meant to provide information on accessing data files when using Fortran 77 in UNIX environments,

STARTING WITH MAINFRAME CODE: Since FORTRAN has the benefit of being quite portable across platforms, most of the code that you currently use on the mainframe will work perfectly well on the UNIX system. The vast majority of the changes that you will need to make (if not all) will be in statements felating to file referencing. The simplest wayu to make the needed changes will be to, first, comment out all direct references to files in the mainframe programs (e.g. any CMS filerefs, etc.). There is no need to change the file referencing numbers in your READ and WRITE statements. "All" that remains to be done after that is adding the appropriate file referencing commands to your code. A skeleton outling of their use is given below, along with a simple example.

OPEN STATEMENTS: Files may be accessed in Fortran 77 using an "OPEN" statement. The general usage is as follows:

OPEN(UNIT=fileref,FILE='filename',OPTIONS)

There are three general parts to the "OPEN" statement. In the first, the user assignsan integer referencing number to the file (unit). In the second, the user provides a file name in the form of a character string. In the third part, the user specifies options p[ertaining to the specific fiule attributes (this part is not always necessary). Tof a given option, one can either hard code the preference into the program, or create variables (either character of integer) that conrtain the pertinent information. For example, the following sets of lines equivalent.

Set 1:

open(unit=1,file='test.dat',status='old')

Set 2:

unit_no = 1
filename = 'test.dat'
file_age = 'old'
open(unit=unit_no,file=filoename,status=file_age)

USAGE NOTE: The "OPEN"statement may be used to access any file you like (provided that the permissions are correctly set). After opening the file, you may read and write to it with the same command syntax that you used on the mainframe, simply by referring to the correct unit number. For example, to read from a test.dat file as in the example above, you could use either

read(1,100) VARIABLE LIST
or
read(unit_no,100) VARIABLE LIST

where 100 is the label for an appropriate FORMAT statement.

IMPORTANT: When you are done accessing the file, the best policy is to close the file immendiately (I have often come to grief by not doing so). The syntax is simple, you merely include the command

close(unit_no).

Remembering, of course, that you simpley use the actual integer value of the file reference.

FILE OPTIONS:

Because the author understands and regularly uses very few of the MANY file options, this will be very brief and sketchy. If you find you need to use more advanced file handling options (and even to understand those listed here), you can read about them in any Fortran 77 handbook. The options will be explained in the section that describes the "OPEN" statement.
NAME:  OPTIONS:  NOTES:

RECL		 RECL=a, where a is an integer (variables allowed). 
		 This is not often needed.  In fact, FORTRAN 77 will
		 not use it, except for files that are not accessed
		 sequentially.

FORM             FORM='formatted' or FORM='unformatted'.

STATUS  old      STATUS='old' - the file to be opened must already exist
	new      STATUS='new' - the file to be opened must not exist

ACCESS  append   ACCESS='append' - append output to the end of the file
        sequential    ACCESS='sequential' - read lines in file sequentially
                              (default)
        direct   ACCESS='direct' - read data from specified line numbers

EXAMPLE: Let's say that you had a program that reads from the text file junk.dat, with lines of 80 characters. Here is one way to access the file as file unit 7.
  1. LINES OF CODE WITHIN A PROGRAM: Add these lines of code to your program, so that they will execute before anything else (or simply when you want to execute the read to get the file information).
    character*12 filename
    integer reclen
    write(*,*) ' Enter the filename.'
    read(*,'(A)') filename
    write(*,*) ' Enter the record length.'
    read(*,*) reclen
    open(unit=7,file=filename,recl=reclen,status='old')

  2. INPUT PARAMETER FILE: Create a file (named "input", for example) that contains these two lines. This is your input parameter file.

    junk.dat
    80

    NOTE: In fact, you do not need to create this parameter file, since it simply contains the strings that you would type when prompted by the program as it executes.

  3. COMPILE YOUR PROGRAM: Compile the program, for example,

    xlf program.f -O -o program

    The -O command optimizes the compilation, making the program execute faster. This is not recommended if you are in a debugging mode, but it can substantially decrease the run time. The -o program command will create an executable file named 'program'. If this option is left off, the compiler will create the executable file and name it 'a.out'. Also, compiling with this command will write all compiler messages to the screen. If you want to save the compiler messages to a file as they are produced, the compiler option "-qsource" will create a listing file. This option creates a file that contains the code, along with compiler errors and other messages. The list file will have the same name as the program name, but with a ".1st" extension, rather than ".f". For example, the command

    xlf program.f -O -o program -qsource

    will save compilation messages in the file 'program.1st'.

  4. EXECUTE THE PROGRAM: Now that you have an executable file, all you need to do to execute the program is to enter the name of the file at the prompt. The program will run, asking you to enter the required parameters. Another option is to feed the contents of the input parameter file into the program upon execution using <. The following two sequences of commands are equivalent. The third is an extension of the second method.
    1. "Direct" Execution. Type the executable's filename, and hit return. EXAMPLE:
      your.prompt% program
      Enter the filename.
      junk.dat
      Enter the record length.
      80

    2. "Automatic" Execution. Type the executable's filename, and hit return. EXAMPLE:

      program < input

      NOTE: A file with the name 'input' must exist, and contain only the lines that the user needs to supply to the program upon execution.

    3. "Batch" Execution. This is the extension of the "Automatic" execution case. Begin in the same way, and after the input file name, add a >, followed by an output file name. End the command by typing &, The > output file name redirects the output from the screen to the output file specified, while the & causes the program to run in "background." Running jobs in the background has the advantage of allowing the user to still use the cursor. This is especially useful if the proram takes a long time to execute. EXAMPLE:

      program < input > output &

      NOTE: A file with the name 'input' must exist, and contain only the lines that the user needs to supply to the program upon execution. In addition, a file named 'output' will be created, containing the messages that the program would have written to the screen. For the example in part (a), the file named 'input' would contain the following two lines:

      junk.dat
      80

      Also, upon execution, a file named 'output' will be created and contain the following two lines:

      Enter the filename.
      Enter the record length

This document simply contains some information that I use regularly. Hopefully it will be useful in getting you started.

Back to SCL Data Points
Contact us at Rice SCL if you have any questions or comments about this page.
Last update on 9/17/96 by Nate Phipps (mrphipps@rice.edu.)