The Queen's University of Belfast
Parallel Computer Centre

[Next] [Previous] [Top]

6 Input/Output


The only major new input/output features in Fortran 90 are NAMELIST, non-advancing I/O and some new edit descriptors.

6.1 Non-advancing I/O

In Fortran 77, every READ or WRITE statement involved complete records. There are occasions where it would be convenient to read/write only part of a record, and read/write the rest later. In Fortran 90, this facility is provided by non-advancing I/O.

Non-advancing I/O obviates the need for records to be read as a whole and for the record length to be known beforehand. It is specified with

ADVANCE='NO'

on the READ or WRITE statement and inhibits the automatic advance to the next record on completion of the statement. If

ADVANCE='YES'

is specified, or the specifier is absent, then the default normal (advancing) I/O occurs.

It is possible to specify advancing and non-advancing I/O on the same record or file. A common use of this is to write a prompt to the screen, specifying non-advancing I/O, and then read the next character position on the screen. For example:

WRITE(*, '("Input size?")', ADVANCE='NO')
READ(*, '(I5)') n

It is often useful to determine how many characters have been read on a non-advancing input. This can be achieved using the SIZE specifier, which has the general form

SIZE=character_count

The integer variable character_count is assigned the number of characters read, excluding any padding characters, on completion of the non-advancing read.

If a non-advancing input reads beyond the end of a record, this can be detected using the IOSTAT specifier which has the form

IOSTAT=io_status

On completion of a READ statement io_status is assigned a value which indicates whether an end-of-record or end-of-file condition has occurred. For example, the NAG compiler returns -1 in the IOSTAT specifier when end-of-file is encountered, and -2 for end-of-record.

6.2 INQUIRE by I/O List

This is used to determine the length of an unformatted output item list. The form

INQUIRE(IOLENGTH=length) output-list

The length may be used as a value of the RECL specifier in subsequent OPEN statements. For example,

INTEGER :: rec_len
...
INQUIRE(IOLEMGTH=rec_len) name,title,age,address,tel
...
OPEN(UNIT=1,FILE='TEST',RECL=rec_len,FORM='UNFORMATTED')
...
WRITE(1) name,title,age,address,tel
...

6.3 NAMELIST

The NAMELIST statement has been available as a suppliers extension to Fortran since the early days (it was available as an IBM extension to FORTRAN II in the early 60's!). It has now been included in the Fortran 90 language. However, NAMELIST is a poorly designed feature and should be avoided whenever possible.

NAMELIST is a facility whereby a set of variables can be gathered together into a named group in order to simplify I/O. The NAMELIST statement is a specification statement and must, therefore, appear before any executable code in the defining program unit. The general form of the NAMELIST statement is:

NAMELIST/namelist-group-name/variable-list

Note that a variable in a NAMELIST group may not be an array dummy argument with non-constant bounds, a variable with assumed character length, an automatic object, an allocatable array, a pointer, or a variable which at any depth of component selection is a pointer.

In READ or WRITE statements, the namelist-group-name may be specified with the NML specifier, or may replace the format specifier. There is no need for input/output lists.

An input record for a namelist group has a specific format:

&namelist-group-name var1=x, var2=y, var3=z

It is possible to omit items when inputting data, and such items remain unchanged. Also, items do not have to be input in the order specified in the NAMELIST statement.

6.3.1 Example

This example shows the namelist group named clothes:

INTEGER :: size=2
CHARACTER (LEN=4) :: colour(3) = (/ ' red','pink','blue' /)
NAMELIST /clothes/ size, colour
WRITE(*, NML = clothes)

The output would be:

&CLOTHES SIZE = 2, COLOUR = redpinkblue/

6.4 New Edit Descriptors

Edit descriptors specify exactly how values should be converted into a character string on an output device or internal file, or converted from a character string on an input device or internal file. Fortran 90 provides the following new edit descriptors:

6.4.1 Example

This example illustrates the differences among E, EN, ES and G edit descriptors:

PROGRAM e_en_es_g_compare
IMPLICIT NONE
REAL, DIMENSION(4) :: &
x=(/1.234, -0.5, 0.00678, 98765.4/)
PRINT '(4E14.3/4EN14.3/4ES14.3/4G14.3)', x, x, x, x
END PROGRAM e_en_es_g_compare

The output would be

0.123E+01	-0.500E+00	0.678E-02	0.988E+05
1.234E+00	-500.000E-03	6.780E-03	98.765E+03
1.234E+00	-5.000E-01	6.780E-03	9.877E+04
1.23	-0.500	0.678E-02	0.988E+05

6.5 New Statement Specifiers

The INQUIRE, OPEN, READ and WRITE statements are not new, but a few new specifiers have been added.

INQUIRE

OPEN

The specifiers POSITION, ACTION, DELIM and PAD have the same values and meanings as for INQUIRE. One additional value has been provided for the STATUS specifier:

READ/WRITE

READ

6.6 Exercises

Look at the programs non_adv.f90, inquire.f90, namelist.f90, edit1.f90, edit2.f90, and io_spec.f90, and run them.

Notice how these programs use new I/O facilities.


[Next] [Previous] [Top]
All documents are the responsibility of, and copyright, © their authors and do not represent the views of The Parallel Computer Centre, nor of The Queen's University of Belfast.
Maintained by Alan Rea, email A.Rea@qub.ac.uk
Generated with CERN WebMaker