Unit numbers

read ([unit=]u, [fmt=]fmt) list

write ([unit=]u, [fmt=]fmt) list

Unit number u

Unit numbers 5 and 6 are usually associated with standard input (read) and output (write, print), respectively.

read (5, *) x
read (*, *) x
read *, x
write (6, *) x
write (*, *) x
print *, x

External files

Connect a file to a unit. File name fln (character expression)

open ([unit=]u, file=fln)

open (unit=1, file='dcl35.dat')
open(1, file='dcl35.dat')

Disconnect a file from a unit

close ([unit=]u)

close (unit=1)
close(1)

Read data up to end of file. Scalar integer variable ios has a negative value after execution of the read statement if an end-of-record or end-of-file condition is detected, a positive value if an (formatting) error is detected, or the value zero otherwise.

read([unit=]u, [fmt=]fmt, iostat=ios) list

integer :: ios
open(1, file=...)
do
  read(1, *, iostat=ios) ...
  if (ios < 0) exit ! end of file
  ...
end do

If a file name is not specified or if the open statement is omitted the system may create a default file (system specific), e.g., execution of a write(3,*) statement may create a file fort.3, for003., or the like.

Internal files

Internal files allow format conversion between various representations in a storage area within the program itself.

An internal file must be of default character type.

Example: External file dcl35.dat

P(11) 1960.94
P(10) 1973.79
P(9) 1986.44
P(8) 1998.90
P(7) 2011.16
P(2) 2069.39
P(1) 2080.40
R(0) 2101.76
R(1) 2112.08
R(6) 2160.20
R(7) 2169.10
R(8) 2177.74
R(9) 2186.13
R(10) 2194.26
R(11) 2202.14
implicit none
integer, parameter :: max = 100 ! max. data
integer, dimension(max) :: m
real, dimension(max) :: w
real :: sx=0., sxx=0., sxy=0., sy=0., a, b, det, y
integer :: i = 0, i1, i2, ios, j, n
character(len=20) :: line
open(1, file='dcl35.dat')
do
  read(1, '(a)', iostat=ios) line
  if (ios < 0) exit ! end of file
  i = i + 1
  i1 = index(line, '(') + 1
  i2 = i1 + index(line(i1:), ')') - 2
  read(line(i1:i2), *) j
  read(line(i2+2:), *) w(i)
  select case (line(1:1))
  case('P')
    m(i) = -j
  ...
end do
n = i

Exercises

1. Linear regression (y = a + bx) of D35Cl IR data (see above).

2. Linear regression (Birge & Sponer) of iodine LIF data.

External file iodine.dat (vib. quantum no., wavelength/nm)

2 544.6
3 551.0
4 557.4
5 563.9
6 570.4
7 577.4
8 584.1
9 591.2
11 605.6
13 620.4
14 627.8
15 635.7
16 643.6
17 651.7
18 659.7
19 667.9
20 676.5