4.1.6. Input/Output

4.1.6.2. Opening, Closing, Reading from and Writing to Files

  • In Fortran each file is associated with a unit number, an integer between 1 and 99.

  • Some unit numbers are reserved (I usually start from 10):

    • 5 is keyboard input

    • 6 is keyboard output

  • iostat will be non-zero if there is an error - can be used to detect the type of error

  • err then specifies the line number with which to proceed should an error be encountered, perhaps to print to the screen. Error lables can start at 1.

Code

Meaning

write(10, '(i10, 2e22.15)', iostat=write_error, err=10) n, velocity, pressure

Write list to unit write(unit, format, specifiers) list

read(10, '(i10, 2e22.15)', iostat=read_error, err=20) n, velocity, pressure

Read list from unit read(unit, format, specifiers) list

open(10, file='data.txt', iostat=open_error, status='unknown', err=30)

Open file open(unit, specifiers)

close(10, iostat=close_error, err=40)

Close file close(unit, specifiers)

4.1.6.3. Format of the File

Take the following read statement:

read(10, '(i10, 2e22.15)', iostat=read_error, err=20) n, velocity, pressure

The format of the corresponding file must match with comma separated values, i.e.:

1, 200.0, 350.0

4.1.6.4. Format Statements (print, read, write)

The key to format statements:

Code

Meaning

w

Full length

m

Minimum digits

d

Decimal places

e

Exponent length

n

Positions to skip

c

Positions to move

r

Repetitions

Code

Meaning

format = "(F10.3, A, ES14.7)"

Format string

Iw Iw.m

Integer form

Bw.m Ow.m Zw.m

Binary, octal, hex integer form

Fw.d

Decimal form real format

Ew.d

Exponential form (0.12..E-11)

Ew.dEe

Specified exponent length

ESw.d ESw.dEe

Scientific form (1.2…E-10)

ENw.d ENw.dEe

Engineering form (123.4…E-12)

Gw.d

Generalized form

Gw.dEe

Generalized exponent form

Lw

Logical format (T, F)

A Aw

Characters format

nX

Horizontal positioning (skip)

Tc TLc TRc

Move (absolute, left, right)

r/

Vert. positioning (skip lines)

r(...)

Grouping / repetition

:

Format scanning control

S SP SS

Sign control

BN BZ

Blank control (blanks as zeros)

4.1.6.5. Data-Transfer Specifiers (Read, Write)

Important specifiers are in bold, typical example:

write(10, '(i10, e22.15, e15.6)', iostat=write_error, err=10) n, velocity, pressure

read(10, '(i10, e22.15, e15.6)', iostat=read_error, err=20) n, velocity, pressure

Code

Meaning

iostat=integer_variable

Save iocode (error) to variable

err=label

Label to jump to on error

advance='yes'
advance='no'

(Non-)advancing data transfer

end=label

Label to jump to on end of file

eor=label

Label for end of record

rec=integer_variable

Record number to read or write

size=integer_variable

Number of characters read

4.1.6.6. I/O Specifiers

4.1.6.6.1. Open Specifiers

Important specifiers are in bold, typical example:

open(10, file='data.txt', iostat=open_error, status='unknown', err=30)

Code

Meaning

file='filename'

Name of file to open, e.g. file = 'data.txt' (if it’s in the same directory as the complied code)

iostat=integer_variable

Save iocode (error) to variable, e.g. iostat = open_error

status='old'
status='new'
status='replace'
status='scratch'
status='unknown'

Status of input file, e.g. status='unknown'

err=label

Label to jump to on error

access='sequential'
access='direct'

Access method

form='formatted'
form='unformatted'

Formatted/unformatted I/O

recl=integer_variable

Length of record

blank='null'
blank='zero'

Ignore blanks/treat them as 0

access='sequential'
access='direct'

Access method

position='asis'
position='rewind'
position='append'

Position, if sequential I/O

action='read'
action='write'
action='readwrite'

Read/write mode

delim='quote'
delim='apostrophe'
delim='none'

Delimiter for char constants

pad='yes'
pad='no'

Pad with blanks

4.1.6.6.2. Close Specifiers

Important specifiers are in bold, typical example (to close the file we opened, with unit 10):

close(10, iostat=close_error, err=50)

Code

Meaning

iostat

Save iocode (error) to variable

err

Label to jump to on error

status='keep'
status='delete'

Status of closed file

4.1.6.7. Reference List

This contains commands I haven’t used, but maybe useful for translation/understanding.

4.1.6.7.1. Getarg, Inquire, Backspace, Endfile, Rewind Functions

Code

Meaning

call getarg(2, var)

Put 2nd CLI-argument in var

inquire(unit, spec)

Inquiry by unit

inquire(file=filename, spec)

Inquiry by filename

inquire(iolength=iol) outlist

Inquiry by output item list

backspace(unit, spec)

Go back one record

endfile(unit, spec)

Write eof record

rewind(unit, spec)

Jump to beginning of file

4.1.6.7.2. Inquire Specifiers

I haven’t used these

Code

Meaning

access
action
blank
delim
direct
exist
form
formatted
iostat
name
named
nextrec
number
opened
pad
position
read
readwrite
recl
sequential
unformatted
write
iolength

4.1.6.7.3. Backspace-, endfile-, rewind-specifiers

I haven’t used these

Code

Meaning

iostat

Save iocode (error) to variable

err

Label to jump to on error