Self-Study Guide 2: Programming in Fortran 95 by Dr. Rachael Padman - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

3. Input to and output from a F95 program

We have already seen some examples of outputting information from a program (write) and reading information from the terminal (read). In this section we will look in detail at input and output and explain those strange ‘*’s. To save some writing let’s introduce some jargon: we will call input and output I/O.

3.1 F95 statements for I/O

Input and output to a F95 program are controlled by the read and write statements. The manner in which this is done is controlled by format descriptors, which may be given as character variables or provided in a format statement. For economy of effort we will only outline the latter method, together with the default mechanism.

The form of the I/O statements is as follows:

read(stream, label [, end=end][, err=err]) list

and

 write(stream, label) list

 

where

 

  • stream is a number previously linked to a file, or a character variable, or *, where

* here indicates the default value, usually the screen of a terminal session. If stream is a character variable, the result of the write is stored in that variable, and can be manipulated as such within the program.

  • label is the number of a format statement, or * for free format.
  • list is a list of items to be transferred, separated by commas, possibly including text strings enclosed in quotation marks.
  • The optional items end and err are so that you can provide statement labels end and err to which control moves in the event that the end of data is reached prematurely (end) , or some error is encountered (err).

The precise details of how the output should look are governed by the format definition. This takes the form:

label format (format descriptors)

  • label is an integer, corresponding to the label appearing in the read or write statement. More than one read or write can refer to the same label.
  • format descriptors is a comma-separated list of items describing how the output is to be presented, possibly including text items. The latter should be enclosed in single quotation marks as in character strings.

img25.png

To access a file for input or output you can use the open statement:

open([unit=]stream, err=escape, action=action, file=name)

There are further possible arguments which should not be needed for this course, but which you may look up in a text book.

  • stream    is the identifier which appears in the read or write statement
  • escape    is a statement label, to which control is transferred if there is a problem opening the file.
  • action    is one of ‘read’, ‘write’ or ‘readwrite’, depending how you intend to use the file.
  • name    is the name of the file (in quotes) and may be held in a character variable.

Having opened a file, linking it to a stream, and read through it, you can move back to the beginning using:

rewind( stream)

When you have completed I/O to a particular file, you can use the close instruction to close the file and tidy things up:

close( stream)

  • Try entering the following example, compile and run it and examine the output

img26.png

  • Modify the program to use “free-format” output and compare the results (you could output both in one program for comparison of course).
  • The next example illustrates how to send the output from your program to a file

img27.png

  • Modify the program to send a copy of the output to two files simultaneously.