List-directed I/O

print * and read * are the two statements of the language that need no format: the compiler picks one for the values it is given, and on input it accepts anything a person would type. That freedom is what makes them the right tool while a program is being written and the wrong tool for a file another program will read. This lesson shows what the freedom produces.

What list-directed output looks like

Each item is written in a field the compiler chooses, with one blank in front of it, and the items of one statement share one record. There is no alignment between statements: the width depends on the type of the value and not on the column a reader would like.

The file listout.f90:

program listout_demo implicit none integer :: i real :: x character(len=6) :: s logical :: ok i = 42 x = 3.5 s = "abc" ok = .true. print *, i print *, x print *, s print *, ok print *, i, x, s, ok print *, "label:", i end program listout_demo

gfortran -Wall -Wextra -std=f2018 listout.f90 -o listout
./listout

42
3.50000000
abc
T
42 3.50000000 abc T
label: 42

An integer gets a field of twelve on this compiler, a real one of thirteen with eight decimals, a string its own length and a logical of two. None of those numbers is in the standard: they are what gfortran chose, and another compiler may choose others. The last line shows that a label is just another item, so the number follows it at the width of an integer.

The widths belong to the compiler

The standard asks for two things only: a separator between the values (a blank or a comma) and enough digits that the value can be read back. Within those rules a compiler may print 3.50000000 or 3.5, and a program that aligns a table with print * is relying on an implementation detail.

That is the whole rule of this lesson: list-directed output is for a person reading a screen, and the moment the output is a file for another program, the format of the previous lesson takes over.

List-directed input

On input the compiler is generous: values may be separated by blanks, commas or both, a comma may be repeated, and a string may be quoted or not. The file listin.f90 reads three records from character variables, which is how a read from a real file behaves without needing one:

program listin_demo implicit none character(len=60) :: line integer :: a, b, c line = "3*7" read (line, *) a, b, c print *, "3*7 gives:", a, b, c line = "1, 2, 3" read (line, *) a, b, c print *, "commas :", a, b, c line = "10 20" read (line, *) a, b print *, "two :", a, b end program listin_demo

gfortran -Wall -Wextra -std=f2018 listin.f90 -o listin
./listin

3*7 gives: 7 7 7
commas : 1 2 3
two : 10 20

Reading from an internal file is the same operation as reading from a unit, which is why the lesson can show it without a data file: the building lesson wrote into such a variable, and this one reads out of it.

Repeat counts and separators

3*7 is three sevens, the same r*c form a DATA statement of old Fortran used, and it is the only place a repeat count appears on input. A quoted string is taken as one item even when it holds blanks, so "New York" 12 is a string and an integer and not three items.

A comma between two values is a separator and nothing more; two commas in a row are a separator with a null value between them, which is the subject of the next section. A comma after the last value of a record is ignored, which is the one place list-directed input is more forgiving than list-directed output.

The slash and null values

A slash ends the read: the items after it are not touched, which is how a program reads the first fields of a record and ignores the rest. A null value leaves its item unchanged, and that is the trap, because unchanged means whatever the variable held before, not zero.

program slash_demo implicit none character(len=40) :: line integer :: a(5), b(3) a = -1 line = "1 2 3 / 4 5" read (line, *) a print *, "a :", a b = -1 line = "1,,3" read (line, *) b print *, "b :", b end program slash_demo

gfortran -Wall -Wextra -std=f2018 slash.f90 -o slash
./slash

a : 1 2 3 -1 -1
b : 1 -1 3

The array keeps its minus ones in the positions the record did not reach, and the null value in the middle of 1,,3 leaves the second element as it was. A program that initialises the array first gets a defined answer; one that does not gets whatever the memory held, and the compiler cannot warn about it.

Exercises

  1. Add print *, 1.0 / 3.0 to the first program. How many digits does the output hold, and can the value be read back exactly?
  2. Print the same real with print *, x, x, x. Where does the second value start, and why?
  3. Read "2*3 4" into four integers. Which values come out?
  4. Read "1 2" into a five-element array that was set to zero first. Which elements stay zero?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does print *, x write for a real?

    Answer

    The value in a field the compiler chooses, with enough digits to read it back

    List-directed output leaves the width and the digits to the compiler.

  2. What separates two items read by read *, a, b?

    Answer

    Blanks, commas or both

    List-directed input accepts blanks and commas as separators.

  3. What does this program print? program p implicit none character(len=20) :: line integer :: a, b, c line = "3*2" read (line, *) a, b, c print *, a + b + c end program p

    Answer

    6

    3*2 is three twos, and their sum is six.

  4. What does the slash in a list-directed read do?

    Answer

    Ends the read and leaves the remaining items unchanged

    The slash terminates the transfer at that point.

  5. What does a null value, two commas in a row, do?

    Answer

    Leaves the corresponding item unchanged

    A null value leaves the variable as it was.

  6. Which statement about the widths of list-directed output is true?

    Answer

    They are chosen by the compiler within the rules of the standard

    The standard requires separators and round-trip digits, not widths.

  7. What does read (line, *) n read?

    Answer

    A value from the character variable line, as from an internal file

    A character variable can be the unit of an internal read.

  8. What is "New York" 12 for a list-directed read of a string and an integer?

    Answer

    Two items: the quoted string and the integer

    A quoted string is one item whatever blanks it holds.

  9. Why is print * the wrong choice for a file another program reads?

    Answer

    The layout is the compiler choice, so it can change from one compiler to the next

    A format fixes the layout; list-directed output does not.

  10. What does print *, "label:", i put between the label and the number?

    Answer

    One blank, then the integer in its field

    Each item is preceded by a separator, normally a blank.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev