An important part of any computer program is to handle input and output. In our examples so far, we
used only the most common Fortran command for output, write
. Fortran input/output processes
can be quite complicated, so we will only describe some simpler cases in this tutorial.
Assume that you have a file containing data from some measurement which we want to use for further
analysis. Before you can use this file you have to open it. The syntax of the open
command is
rather complicated, but for our purpose it suffices to write:
open(unit, FILE='file_name', STATUS='status')The
unit
number is a number in the range 9-99 that denotes this file; you may choose any
number but make sure you do not open two files with the same specified unit number. The
file_name
gives the name of the file which we are going to work with,
e.g. FILE='fourier.dat'
. Finally, status defines whether the file exists and has to be opened
(STATUS='old'
), it does not exist and will be created (STATUS='new'
), it will
be created or replaces the old file if exists (STATUS='replace'
), or it is a scratch
file which will be automatically deleted after closing it (STATUS='scratch'
). Note that if
you specify STATUS='new'
and the file you are going to create already exists, the execution
of the program ends up with an error. For this purpose it is convenient to use
STATUS='replace'
which first deletes the old file (if exists) and then creates and opens a
new one. An opened file can be closed by the command
close(unit)where the
unit
number identifies the file you want to close. In the case when the data are
read from the terminal (input of data from user or standard output), you do not need to use
open
and close
commands at all. The unit number for this standard input/output is
substituted by symbol *
, e.g. write(*,*) number
displays the value of the variable
number
.
After a file has been opened with command open
, you can access its contents using the command
read
. If the file is new and you want to store some data in it, use command write
instead. The structure of these two commands is very similar:
read(unit,format) list_of_variables write(unit,format) list_of_variablesHere,
unit
is the unit number of the file we work with, format
specifies the form in
which the data will be read/written and the list_of_variables
provides the variables for
storage the data read from the file (when reading) or the variables whose contents will be saved in
file (when writing).
F77 recognizes two forms of output, namely unformatted and formatted. The former
simply allows you to write data in a file or in the standard output (terminal) without specifying
the format
in the command write
. For example, write(*,*) number
prints the
value stored in the variable number
in your terminal window. The latter, formatted output,
allows you to specify a particular format in which the data will be written/read. This is very
useful to keep the outputs from your program neat for further analyses. The format can be generally
built by mixing text and the so-called descriptors. Each descriptor defines the appearance of
the output of exactly one variable given behind read
or write
command. Almost complete
set of descriptors which you might use for defining formats in your programs is given in the table.
descriptor | meaning |
I |
output an integer in the next character positions |
F |
output a real number in the next character positions with decimal places |
E |
output a real number in the next character positions using an exponent format with decimal places in the mantissa and four characters for the exponent |
A |
output a character string in the next character positions; if is omitted, the output will start at the next available position with no leading or trailing blanks |
L |
output T for true or F preceded by blanks |
X |
ignore the next character positions ( times space) |
"text" |
write the text |
When using more than one descriptor, they must be separated by commas. Repetition of a certain part
of the formatting pattern can be guaranteed by giving the number of repetitions, followed by a
bracket containing the format to be repeated. For example: 3(F5.2,3X)
means the same as
(F5.2,3X,F5.2,3X,F5.2,3X)
. Check the examples below to get a better feel about how the format
can be specified.
Example: Demonstration of the formatted output. The asterisks in the comments on the right are substitutes for blanks.
pi = 3.1415927 eval = 0.006272985 flag = .true. write(*,'(F10.4)') pi ! output: '****3.1416' write(*,'("pi = ",F8.6)') pi ! output: 'pi = 3.141593' write(*,'(E6.2)') eval ! output: '**6.27E-03' write(*,'("This is ",L)') flag ! output: 'This is T' write(*,'(2(F4.2,2X,L,3X))') pi,flag,pi,flag ! output: '3.14**T***3.14**T***'
The same rules for specifying formats hold also for reading from file,
e.g. read(10,'(F5.4)') rnum
, and for reading from the standard input,
e.g. read(*,'(A80)') text
.
Roman Gröger (2015-09-23)