[Next] [Previous] [Top]
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
on the READ or WRITE statement and inhibits the automatic advance to the next record on completion of the statement. If
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
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
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.
The length may be used as a value of the RECL specifier in subsequent OPEN statements. For example,
INTEGER :: rec_len
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:
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.
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/
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
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:
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.