List-Directed Statements [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP FORTRAN 77/iX Programmer's Guide
List-Directed Statements
List-directed I/O, sometimes called "free format" I/O, is the simplest
kind of formatted I/O. List-directed I/O lets the compiler select a
format for the I/O data, depending upon the type and magnitude of the
data in the variable list on the I/O statement (therefore the name
list-directed). List-directed I/O is selected by an asterisk where the
format specification normally appears.
List-Directed Input
A list-directed READ statement can be written in one of two ways. First,
the READ statement can specify the input unit number for the source of
the input data. For example,
READ(5,*) i, j, k
Second, the READ statement does not have to specify the unit number;
instead, the input data is taken from the standard input device. For
example,
READ *, i, j, k
Data items read by a list-directed READ statement can be separated by a
comma, blanks, or can be on separate lines. This flexibility makes
list-directed input convenient for you to enter data.
The input field can be terminated abruptly by entering a slash ( / ) in
the input field, causing any remaining items in the I/O list to be
skipped. For example, if the input line:
5 /
is read by one of these statements:
READ(5,*) i, j, k
READ *, i, j, k
the variable i is assigned the value 5, and the read terminates. The
variables j and k are unchanged. Entering a slash is useful when you
want to enter only the first few values of a long input list.
List-directed input data can contain a multiplier to enter many copies of
an input value. For example, the input line:
3*1024
assigns the value of 1024 to three input variables.
Character data read with a list-directed READ statement must be enclosed
in quotation marks if the data contains any of the following separators:
blank ( ), comma (,) or slash (/). This is because list-directed input
uses blanks, commas, and slashes as data separators. (If the input field
is specified by format descriptors, apostrophes are not required.) For
example, the statements:
INTEGER*4 id, section
CHARACTER*10 name
READ *, id, name, section
accept the following input string:
2612 'J. Smith' 7
As an HP extension to the FORTRAN 77 standard, data can be read from an
internal file with a list-directed READ.
List-Directed Output
A list-directed output statement can be written in one of two ways.
First, the WRITE statement must have a unit number for the destination of
the output data. For example,
WRITE(6,*) 'Output values=', i, j, k
Second, the PRINT statement always writes to the standard output device.
For example,
PRINT *, 'Output values=', i, j, k
There is no WRITE statement equivalent to the PRINT statement form. That
is, this statement is illegal:
WRITE *, i, j, k
You can include a comma before the list of data items in the WRITE
statement, as shown below:
WRITE(6,*),a,b,c
List-directed output prints numeric data with a leading blank. Character
data is printed without any leading blanks.
Numeric data can be printed in scientific notation, depending upon the
magnitude. However, you should not use list-directed output for
applications where the exact format of the output data is critical.
The program below illustrates the two ways of writing list-directed input
and output statements.
PROGRAM list_directed_io
C List-directed input statements:
READ(5,*) i1 ! Input from the preconnected input unit.
READ *, i2 ! Input from the standard input device.
C List-directed output statements:
WRITE(6,*) 'i1=', i1 ! Output to the preconnected output unit.
PRINT *, 'i2=', i2 ! Output to the standard output device.
END
Following is another program with list-directed output statements:
PROGRAM output_ex
COMPLEX vector
CHARACTER string*8
vector = (1.0, 1.0)
string = 'alphabet'
int = 123
var = 123.456E29
PRINT *, vector, string, int, var
END
The output of this program is the following line:
(1.0,1.0)alphabet 123 1.23455E+31
Note that blanks are inserted before numeric values and not before
character strings. List-directed I/O can also be done on nonstandard
files, as described in Chapter 3 . In particular, list-directed WRITE
statements can use internal files.
MPE/iX 5.0 Documentation