next up previous contents
Next: Some frequently occurring errors Up: A short summary of Previous: Subprograms   Contents


Input and output

FORTRAN has many facilities for reading from and writing to input and output devices. Only the necessary minimum is discussed here.
The read command is:
      read *, <variable list>
The <variable list> is a sequence of variables, separated by commas. The numbers which are read must be separated by one or more spaces, a comma or the end of the line. Each new read command will read from a new line.
Example:
      read *, a, b, c
The <variable list> can also be a so-called implied do-statement.
Example:
      read *, (a(i), i = 1, 100)
The variables a(1) t/m a(100) are read in.


The write command is:
      write *, <output list>
The <output list> is a sequence of variables, separated by commas.
Example:
      print *, 'a = ',a
The <output list> can also be an implied do-statement like for the read command.
Example:
      print *, (a(i), i = 1, 100)
The implied do-statement is used for reading and writing of an array.
Example:
      program example
      implicit none
      integer i, j
      double precision a(1:100,-2:2)
      do i = 1, 100
         read *, (a(i,j),j=-2,2)
      end do
      print *, 'The array A is:'
      do i = 1, 100
         print *, (a(i,j),j=-2,2)
      end do
The input file can be for instance the following:
 1 2 4 2 1         first line in the file (= first row of a)
 1 2 4 2 1         second line in the file (= third row of a)
   .
 1 2 4 2 1         fourth line in the file (= fourth row of a)
   .
   .
   .
The output then becomes:
The array A is:
 1 2 4 2 1
 1 2 4 2 1
   .
 1 2 4 2 1
   .
   .
   .


Mathieu Pourquie 2001-02-28