Practical Fortran
File I/O
Scratch Unit
A scratch file is a unit with no name that is opened and discarded automatically. Useful for short-lived buffers.
Program
Play the program to write a number to a scratch unit, rewind, read it back, and double it.
file_io.f90
program file_io_demo
implicit none
integer :: unit_, value_in, value_out
value_in = 42
open(newunit=unit_, status='scratch', action='readwrite')
write(unit_, '(I0)') value_in
rewind(unit_)
read(unit_, *) value_out
close(unit_)
print '(I0)', value_out * 2
end program file_io_demo
scratch
`status='scratch'` opens a temporary unit deleted on close.
newunit
`newunit=unit_` picks an unused unit number.
rewind
`rewind` returns the file position to the start before reading.