Fortran Formats

We have discussed the READ and WRITE statements. These are the so-called list-directed input/output statements. They are also referred to as free-format input/output statements. List-directed input/output statements are easy to use; however, we have no control over the appearance of the input and output. To overcome this problem, we should use format s.

Fortran formats are used to control the appearance of the input and output. It has the following simple form:

( . format edit descriptors . )

That is, a Fortran format is a pair of parenthesis that contains format edit descriptors separated by commas.

READ(*,'(2I5,F10.2)') . variables . READ(*,"(5F10.2)") . variables . WRITE(*,'(A,I5)') . variable and expressions . WRITE(*,"(10F5.2)") . variable and expressions .
CHARACTER(LEN=20), PARAMETER :: FMT1 = "(I5,F10.2)" CHARACTER(LEN=*), PARAMETER :: FMT2 = "(4I5, 5E14.7, 8F5.0)" READ(*,FMT1) . variables . READ(*,FMT1) . variables . WRITE(*,FMT2) . variables and expressions . WRITE(*,FMT2) . variables and expressions .
CHARACTER(LEN=80) :: String String = "(3I5, 10F8.2)" READ(*,String) . variables . WRITE(*,String) . variables and expressions .
WARNING: The length of the string which contains a format must be large enough. Otherwise, the format stored there becomes incomplete and causes format error. Consider the following example.
CHARACTER(LEN=10) :: FMT FMT = "(I2,F3.5,E15.7)" WRITE(*,FMT) .
Since FMT has length 10 and the format contains 15 characters, what FMT can actually have is
(I2,F3.5,E
which is not a complete format.

Format Edit Descriptors

The tedious part of using Fortran format is to master many format edit descriptors. Each edit descriptor tells the system how to handle certain type of values or activity. Each value requires some position s. For example, an integer of four digits requires at least four positions to print. Therefore, the number of positions to be used is the most important information in an edit descriptor.

The following are the editor descriptors to be discussed. Details will be given on subsequent pages.

Purpose Edit Descriptors
Reading/writing INTEGER s I w I w.m
Reading/writing REAL s Decimal form F w.d
Exponential form E w.d E w.d E e
Scientific form ES w.d ES w.d E e
Engineering form EN w.d EN w.d E e
Reading/writing LOGICAL s L w
Reading/writing CHARACTER s A A w
Positioning Horizontal n X
Tabbing T c TL c and TR c
Vertical /
Others Grouping r (. )
Format Scanning Control :
Sign Control S , SP and SS
Blank Control BN and BZ

Most edit descriptors can be repeated and several edit descriptors can be grouped into a group. For most of the cases, edit descriptors are separated by commas. The following is an example:

CHARACTER(LEN=30) :: Format Format = "(5X, I5.2, F10.3, A, ES14.7)" READ(*,Format) . variables . WRITE(*,Format) . variables and expressions .

In the above example, format Format has five edit descriptors 5X , I5.2 , F10.3 , A and ES14.7 . Adjacent edit descriptors are separated by a comma.

IMPORTANT: You can use both listed-directed and formatted READ s and WRITE s in your program.