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 formats.
Fortran formats are used to control the appearance of the input and output. It has the following simple form:
That is, a Fortran format is a pair of parenthesis that contains format edit descriptors separated by commas.( ..... format edit descriptors ..... )
There are three possible ways to prepare a Fortran format. Fortran has a FORMAT statement; but, we will not use it because the two methods discussed below offer higher level of flexibility.
The above has two READ and two WRITE statements whose second asterisks are replaced with format character strings. 2I5, F10.2, 5F10.2, A,I5 and 10F5.2 are format edit descriptors.READ(*,'(2I5,F10.2)') ... variables ... READ(*,"(5F10.2)") ... variables ... WRITE(*,'(A,I5)') ... variable and expressions ... WRITE(*,"(10F5.2)") ... variable and expressions ...
In the above, character constants (defined as PARAMETERs) FMT1 and FMT2 are used as formats.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 ...
Note that the same format can be used in both READ and WRITE statements.CHARACTER(LEN=80) :: String String = "(3I5, 10F8.2)" READ(*,String) ... variables ... WRITE(*,String) ... variables and expressions ...
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
Since FMT has length 10 and the format contains 15 characters, what FMT can actually have isCHARACTER(LEN=10) :: FMT FMT = "(I2,F3.5,E15.7)" WRITE(*,FMT) ......
which is not a complete format.(I2,F3.5,E
We shall use the following convention of symbols:
The following are the editor descriptors to be discussed. Details will be given on subsequent pages.
|Reading/writing REALs||Decimal form||Fw.d|
|Tabbing||Tc||TLc and TRc|
|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:
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.CHARACTER(LEN=30) :: Format Format = "(5X, I5.2, F10.3, A, ES14.7)" READ(*,Format) ... variables ... WRITE(*,Format) ... variables and expressions ...
|IMPORTANT: You can use both listed-directed and formatted READs and WRITEs in your program.|