FORTRAN 77 Language Reference

Direct Access I/O

If your I/O lists are different lengths, you can OPEN the file with the RECL=1 option. This signals FORTRAN to use the I/O list to determine how many items to read or write.

For each read, you still must tell it the initial record to start at, in this case which byte, so you must know the size of each item. @

A simple example follows.

Example: Direct access--create 3 records with 2 integers each:


demo% cat Direct1.f
	integer u/4/, v /5/, w /6/, x /7/, y /8/, z /9/
	open( 1, access='DIRECT', recl=8 )
	write( 1, rec=1 ) u, v
	write( 1, rec=2 ) w, x
	write( 1, rec=3 ) y, z
	end
demo% f77 -silent Direct1.f
demo% a.out
demo%

Example: Direct access--read the 3 records:


demo% cat Direct2.f
	integer u, v, w, x, y, z
	open( 1, access='DIRECT', recl=8 )
	read( 1, rec=1 ) u, v
	read( 1, rec=2 ) w, x
	read( 1, rec=3 ) y, z
	write(*,*) u, v, w, x, y, z
	end
demo% f77 -silent Direct2.f
demo% a.out
  4  5  6  7  8  9
demo%

Here we knew beforehand the size of the records on the file. In this case we can read the file just as it was written.

However, if we only know the size of each item but not the size of the records on a file we can use recl=1 on the OPEN statement to have the I/O list itself determine how many items to read:

Example: Direct-access read, variable-length records, recl=1:


demo% cat Direct3.f
	integer u, v, w, x, y, z
	open( 1, access='DIRECT', recl=1 )
	read( 1, rec=1 ) u, v, w
	read( 1, rec=13 ) x, y, z
	write(*,*) u, v, w, x, y, z
	end
demo% f77 -silent Direct3.f
demo% a.out
  4  5  6  7  8  9
demo%

In the above example, after reading 3 integers (12 bytes), you start the next read at record 13.