Unformatted I/O is used to transfer binary information to or from memory locations without changing its internal representation. Each execution of an unformatted I/O statement causes a single logical record to be read or written. Since internal representation varies with different architectures, unformatted I/O is limited in its portability.
You can use unformatted I/O to write data out temporarily, or to write data out quickly for subsequent input to another FORTRAN program running on a machine with the same architecture.
Logical record length for unformatted, sequential files is determined by the number of bytes required by the items in the I/O list. The requirements of this form of I/O cause the external physical record size to be somewhat larger than the logical record size.
WRITE( 8 ) A, B
The FORTRAN runtime system embeds the record boundaries in the data by inserting an INTEGER*4 byte count at the beginning and end of each unformatted sequential record during an unformatted sequential WRITE. The trailing byte count enables BACKSPACE to operate on records. The result is that FORTRAN programs can use an unformatted sequential READ only on data that was written by an unformatted sequential WRITE operation. Any attempt to read such a record as formatted would have unpredictable results.
Here are some guidelines:
Avoid using the unformatted sequential READ unless your file was written that way.
Because of the extra data at the beginning and end of each unformatted sequential record, you might want to try using the unformatted direct I/O whenever that extra data is significant. It is more significant with short records than with very long ones.
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.
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%
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:
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.