Here are some basic read and write:
To read in a file called data_in
with N
lines and variables x
, y
, z
in each line:
open(unit=10,file='data_in',status='old')
do i=1,N
read(10,*) x(i), y(i), z(i)
end do
close(10)
To write variables x
, y
, z
of dimension N
each to a file called data_out
:
open(unit=10,file='data_out',status='unknown')
do i=1,N
write(10,*) x(i), y(i), z(i)
end do
close(10)
To take x
, y
and z
as input from the keyboard (standard input, *
) asking the user before hand:
do i=1,N
write(*,*) 'Please input element number ', i, ' of variables x, y and z, respectively'
read(*,*) x(i), y(i), z(i)
end do
In each of the three cases, the first option in read
and write
is the input/output unit, either referred to with a number (I have used 10
after opening the files with that identifier) of with an asterisk *
, which signifies "standard input/output", the terminal prompt in this case. The second option, for which I have used an asterisk *
in all cases refers to the format. *
is the format "as given".