Fortran’s input and output is built on units, numbers that stand for files or devices, and on formats that say how values become text. Everything in the statements below is in the standard unless marked as an extension. Formats are on Format edit descriptors.
Units and preconnected files
A unit is a non-negative integer. Units are connected to files by open, or are preconnected: by long convention unit 5 is the standard input, 6 the standard output and 0 the standard error (the
standard does not give the numbers, but iso_fortran_env does, as input_unit, output_unit and error_unit). A * in place of a unit
means the standard input for read and the standard output for write. open(newunit=u) (Fortran 2008) picks a free negative unit number.
The statements
open (unit=10, file='data.txt', status='old', action='read', iostat=ios)
read (10, *, iostat=ios) x, y, z ! list-directed
read (10, '(3f10.3)') a, b, c ! formatted
read (10, 100, end=900, err=910) n, (v(i), i = 1, n)
write (6, '(a, i5)') 'count = ', n
write (*, *) 'free format'
print '(f8.2)', total ! unit * for output
print *, 'also free format'
rewind (10)
backspace (10)
endfile (10)
flush (10) ! Fortran 2003
inquire (file='data.txt', exist=found)
close (10, status='keep')
wait (unit=10, id=request)
read,write:read (control-list) items; the control list holds the unit, the format and the specifiers below.read format, itemsandprint format, itemsare short forms with unit*printwrites to the standard output; it has no unit. DEC and GNU Fortran also havetypeas another name forprintandacceptfor a read from the standard input (extensions)- an item list holds variables for
readand expressions forwrite; an implied do(v(i), i = 1, n)repeats items; whole arrays and array sections are allowed rewind,backspaceandendfileposition a sequential file;flushpushes buffered output out;waitfinishes an asynchronous transfer
Control specifiers
| Specifier | Used by | Meaning |
|---|---|---|
unit= | all | the unit; may be written as the first item without unit= |
fmt= | read, write | a format: a label, a character expression, or * for list-directed; may be the second item without fmt= |
nml= | read, write | a namelist name instead of a format |
rec= | read, write | the record number of a direct-access file |
pos= | read, write | the file position of a stream-access file (Fortran 2003) |
iostat= | all | an integer: 0 on success, iostat_end (a negative value) at the end of the file, iostat_eor at the end of a record, another value on error |
iomsg= | all | a character variable that receives the text of the error (Fortran 2003) |
err=, end=, eor= | read, write | labels to jump to on an error, at the end of the file or of a record |
advance= | read, write | 'yes' or 'no': non-advancing I/O leaves the position inside the record, for prompts and for reading a line in pieces |
size= | read | the number of characters read by non-advancing input |
asynchronous=, id= | read, write | asynchronous transfer and its request number (Fortran 2003) |
blank=, decimal=, delim=, pad=, round=, sign= | read, write | override the connection’s defaults for this transfer (Fortran 2003) |
The viewer colours these names as keywords inside a control list when they are followed by =, so a variable called size or end elsewhere stays plain.
OPEN specifiers
| Specifier | Values |
|---|---|
file= | the file name |
status= | 'old' (must exist), 'new' (must not), 'replace', 'scratch' (a temporary file), 'unknown' |
access= | 'sequential', 'direct' (records by number, with recl=), 'stream' (a plain byte stream, Fortran 2003) |
form= | 'formatted' (text), 'unformatted' (binary records) |
action= | 'read', 'write', 'readwrite' |
position= | 'asis', 'rewind', 'append' |
recl= | record length (direct access); the unit is compiler-dependent for unformatted files (often bytes, sometimes 4-byte words) |
blank= | 'null' or 'zero': how blanks in numeric input are read |
delim= | 'apostrophe', 'quote', 'none': how list-directed and namelist output delimits character values |
pad= | 'yes', 'no' |
decimal=, round=, sign= | decimal comma or point, rounding mode, sign printing (Fortran 2003) |
encoding= | 'utf-8' or 'default' (Fortran 2003) |
asynchronous= | 'yes' or 'no' |
newunit= | a variable that receives a free unit number (Fortran 2008) |
iostat=, iomsg=, err= | error reporting |
| extensions | convert= (byte order of unformatted files, GNU and Intel), carriagecontrol=, shared, readonly, defaultfile=, disp=/dispose=, buffered=, blocksize= (DEC and Intel) |
close(unit, status='keep'|'delete'|'unknown') closes a unit, and a file opened as 'scratch' is deleted when it is closed.
Kinds of file
- Formatted sequential: text lines. The default. List-directed and format-directed transfers work on it
- Unformatted sequential: the program’s own binary data in records, which most compilers frame with a length before and after each record (4 bytes in GNU Fortran and Intel Fortran by default). The layout is not in the standard, so files are not portable between compilers without care
- Direct access: records of one length, addressed by
rec= - Stream access (Fortran 2003): a plain sequence of bytes, formatted or unformatted, with no record markers. It is the way to read and write a file with a given layout (
access='stream', form='unformatted') - Internal files: a character variable (or array) that stands as the unit:
write (line, '(i5)') nconverts, andread (line, *) xparses
List-directed and namelist input and output
With * as the format the processor chooses the layout. On input, values are separated by blanks, commas or a slash; / ends the list, 3*0.5 repeats a value, a null value between commas leaves
a variable as it was, and character values may be quoted. On output each value takes the width its type needs, with a leading blank.
namelist /params/ n, tol, name
read (10, nml=params) ! reads ¶ms n=5, tol=1e-6, name='run' /
write (6, nml=params)
A namelist group names variables so that a file can set them as name=value pairs between &group and / ($group … $end in older code). It was an extension of most FORTRAN 77 compilers and is standard from Fortran 90.
INQUIRE
inquire (unit=u, …) or inquire (file=name, …) or inquire (iolength=n) items returns the state of a unit or file. Specifiers:
exist, opened, number, named, name, access, sequential, direct, stream, form, formatted, unformatted, action,
read, write, readwrite, position, recl, nextrec, size, blank, pad, delim, decimal, round, sign, encoding,
asynchronous, pending, id, pos, iostat, iomsg and err.
Errors and end of file
do
read (u, *, iostat=ios) x
if (ios == iostat_end) exit ! end of file
if (ios /= 0) stop 'read error'
total = total + x
end do
Without iostat=, err= or end= an error stops the program. Intrinsic functions is_iostat_end(ios) and is_iostat_eor(ios) (Fortran 2003) test the codes.
User-defined derived-type input and output
A type can bind generic :: write(formatted) => … (Fortran 2003) to a subroutine with a fixed argument list, so that print *, x and formatted write statements can write a derived type
with the DT edit descriptor or in list-directed form. See Derived types and objects.
Older and vendor I/O
- FORTRAN 66 had
read,write,printandpunch, with the unit and format given positionally (READ (5, 100) A,READ 100, A), andend file,rewind,backspace; FORTRAN 77 addedopen,close,inquire, direct access, internal files and list-directed input and output - DEC and VAX:
encodeanddecode(internal-file I/O),accept,type,rewrite,delete,find,unlockfor indexed and relative files,$and\edit descriptors that suppress the newline - Many programs open
'/dev/stdout','CON'or'CONSOLE'by name; none of this is standard