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.
- 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')
- 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.
- 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'.
- 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.
- "Direct" Execution. Type the executable's filename, and hit return.
EXAMPLE:
your.prompt% program
Enter the filename.
junk.dat
Enter the record length.
80
- "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.
- "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.)