# Fortran/io

## Introduction[edit]

It is often useful, in Fortran and other languages, to specify where, and how you want something to print or be read. Fortran offers many formatting specifications which can serve these purposes. In this section we will be considering the READ and WRITE statements primarily.

## Read[edit]

The Read statement is a statement which reads from the specified input in the specified form, a variable. For example,

```
PROGRAM READA
IMPLICIT NONE
INTEGER :: A
READ(*,*) A
END PROGRAM
```

will create an integer memory cell for A, and then it will read a value with the default formatting from the default input and store it in A. The first * in (*,*) signifies where the value should be read from. The second * specifies the format the user wants the number read with. Let us first discuss the format strings available. In Fortran we have at our disposal many format strings with which we may specify how we want numbers, or character strings to appear on the screen. For fixed point reals: Fw.d; w is the total number of spaces alloted for the number, and d is the number of decimal places. The decimal place always takes up one position. For example,

```
PROGRAM READA
IMPLICIT NONE
REAL :: A
READ(*,'(F5.2)') A
END PROGRAM
```

Will store, in A, a real number with 2 places before the decimal point, and 2 places after the decimal point. If we input 1.2354, the number would be rounded to 1.24

For Integers: nIw; n is the total number of integers, and w is the number of spaces for each integer. This can be useful for storing strings of digits (10I1). If the Integer does not fit in the allocated space, it will be truncated. For example, if we changed REAL to INTEGER , and F5.2 to I2 in READA above, and then we input 12345, the number 12 would be stored in A.

For Exponential Reals: Ew.d; w is the length of the number, and d is the number of decimal places in the real component. Important things to note are that: (1) the +/- takes up 1 space (2) The first zero in the real component takes up 1 space (3) The decimal point takes up 1 space (4) The E takes up 1 space (5) The the +/- in the exponent takes up 1 space (6) The exponent takes up 2 spaces. Thus, w must be greater than 7+d, or the number will not be stored properly.

Finally, for Character strings: Aw; w is the number of characters you would like