next up previous contents
Next: Formats Up: Input and Output. Previous: Input and Output.   Contents

List-Directed Input and Output

List directed input is read in from the input stream (for now, the standard input device, usually the keyboard) using

read *, input_list
List directed output sends the values of the output list to the output stream (for now, the standard output, usually the display):
print *, output_list
Conceptually, an input (or output) stream is a long sequence of characters, grouped in records; usually, one record = one line, and is ended by the end-of-line character; (it is possible to specify records of arbitrary length, however, they will be ended by the end-of-record character). The characters in a record are read (or written) in sequential order; each stream has a pointer which indicates the current position in the stream; the next read bring in the character whose position in the stream is specified by the stream pointere (next write puts a character in the stream, at the position indicated by the pointer).

To illustrate this, suppose we want a program that reads in the radii of 3 circles from the keyboard, and prints out the perimeters and the areas of the circles. This program might be

program io
  real :: r1,r2,r3,a1,a2,a3,p1,p2,p3
  real, parameter :: pi=3.1415926 
  print *,"give 3 radii" 
  read *, r1, r2, r3 
  p1 = 2.0*pi*r1; p2 = 2.0*pi*r2; p3 = 2.0*pi*r3 
  a1 = pi*r1*r1; a2 = pi*r2*r2; a3 = pi*r3*r3
  print *,"areas = ",a1,a2,a3
  print *,"perimeters = ",p1,p2,p3
end program io
The first  PRINT * statement sends the string ``Give 3 radii'' to the standard output device (presumably the display). The star * following the PRINT means that the output data is the edited using the implicit format.

The READ * statement reads 3 numerical values from the keyboard, and assigns them to r1, r2 and r3. When providing the numbers, the user can separate them by blanks or commas,

1.1  2.2  3.3   or   1.1, 2.2, 3.3
The end of the list is either the end of line character or a slash (/); therefore, if we enter
1.1  2.2 <EOL>    or    1.1 2.2 / 3.3
in either case the first 2 values will be read in and the 3rd will be ignored; r3 will be left unmodified by the READ statement.

The last PRINT * statement outputs the values of the three areas,

Areas = 3.80132723,  15.2053089,  34.2119408
Note that numerical values are rounded to 8 fractional digits and displayed, the default format of the current system. If we want to have more control over the form of the displayed quantities, we need to use formatted output; similarly, formatted input gives control over the read in data.

next up previous contents
Next: Formats Up: Input and Output. Previous: Input and Output.   Contents
Adrian Sandu 2001-08-26