Often it is useful to input data from and output data to files rather than inputting data from the keyboard (standard input) and outputting data to the screen (standard output). The following is a brief introduction to opening and closing files. For more details consult a Fortran reference guide.
In order to use external files it is necessary to open them at the beginning of the program. The following commands would open the files input.dat and output.dat:
open(unit = 7,file = 'input.dat') open(unit = 8,file = 'output.dat')
Opening and closing files can be compiler dependent so you may have to include
other options when opening files. One such variable is the status variable which can take on
various values. For the sake of simplicity, you may have declare your open files as follows:
open(unit = 7,file = 'input.dat',status='old') open(unit = 8,file = 'output.dat',status='new')
Note that it is not necessary to give the files names but this is common practice. If the file ='name' part of the statement was not included, the names of these files would default to fort.7 and fort.8.
The following command would close the file assigned to unit = 7:
close(unit = 7)After a file is closed, the file is disconnected from that unit.
For example, consider the following simple program:
program open_close c implicit none double precision a c c..... Assign a value to a c a = 1.0d0 c c..... open the file output.dat and assign it to unit 7 c open(unit=7,file='output.dat') c c.....Write a out to the output file c write(7,*) a c c..... Close file output.dat c close(7) c c..... Write a out to unit=7 c write(7,*) a c end
What do you think happens when you run this program?
Rewrite your program from Exercise 4(b) so that you read the input (ie. option to calculate from Cartesian to Spherical polar or vice versa plus the three coordinates) from a file called input.dat, choose unit = 7 for this file, and output your answers to a file called output.dat, choose unit = 8 for this file.
The keyboard is known as the standard input and by default it is assigned to unit = 5 while the screen is known as the standard output and by default if assigned to unit = 6. Therefore, the following two read statements are equivalent:
read *, a read(5,*) a
and the following two statements are also equivalent:
print *,a write(6,*) a
Often programs are written to use the standard input and output but the input and output are redirected away from the screen and from the keyboard using the UNIX redirect options. For example, if you have the following program:
program redirect_example c implicit none double precision a, b c c.....Read a and b in from standard input. c read(5,*) a read(5,*) b c c.....Print a and b to standard output c.....You can use either print or write c write(6,*) a write(6,*) b c endNow after compiling you could run the program one of two ways:
./a.outUsing this method you would enter a and b from the keyboard and then they would be outputted to the screen.
./a.out <input.dat >output.datThis reads a and b from the file input.dat and outputs a and b to the file output.dat.
Your program from Exercise 4(b) should have been written so that it read in from the standard input (unit = 5) and ouput to the standard output (unit = 6). Run your program using the redirect options.
Previous: Parameter Statements and User Defined Functions
Return to Dr. Alex Brown's Teaching
This page maintained by alex.brown@ualberta.ca of the Department of Chemistry, University of Alberta
Last updated August 8, 2003.