Most typical Fortran I/O is done to disk files. However, by associating a logical unit number to a physically mounted tape drive via the OPEN statement, it is possible to do I/O directly to tape.
It could be more efficient to use the TOPEN() routines rather than Fortran I/O statements to do I/O on magnetic tape.
With the nonstandard tape I/O package (see topen(3F)) you can transfer blocks between the tape drive and buffers declared as Fortran character variables. You can then use internal I/O to fill and empty these buffers. This facility does not integrate with the rest of Fortran I/O and even has its own set of tape logical units. Refer to the man pages for complete information.
The Fortran I/O statements provide facilities for transparent access to formatted, sequential files on magnetic tape. There is no limit on formatted record size, and records may span tape blocks.
Using the Fortran I/O statements to connect a magnetic tape for unformatted access is less satisfactory. The implementation of unformatted records implies that the size of a record (plus eight characters of overhead) cannot be bigger than the buffer size.
As long as this restriction is complied with, the I/O system does not write records that span physical tape blocks, writing short blocks when necessary. This representation of unformatted records is preserved (even though it is inappropriate for tapes) so that files can be freely copied between disk and tapes.
Since the block-spanning restriction does not apply to tape reads, files can be copied from tape to disk without any special considerations.
A Fortran data file is represented on tape by a sequence of data records followed by an endfile record. The data is grouped into blocks, with maximum block size determined when the file is opened. The records are represented in the same way as records in disk files: formatted records are followed by newlines; unformatted records are preceded and followed by character counts. In general, there is no relation between Fortran records and tape blocks; that is, records can span blocks, which can contain parts of several records.
The only exception is that Fortran does not write an unformatted record that spans blocks; thus, the size of the largest unformatted record is eight characters less than the block size.
An end-of-file record in Fortran maps directly into a tape mark. In this respect, Fortran files are the same as tape system files. But since the representation of Fortran files on tape is the same as that used in the rest of UNIX, naive Fortran programs cannot read 80-column card images on tape. If you have an existing Fortran program and an existing data tape to read with it, translate the tape using the dd(1) utility, which adds newlines and strips trailing blanks.
Example: Convert a tape on mt0 and pipe that to the executable ftnprg:
demo% dd if=/dev/rmt0 ibs=20b cbs=80 conv=unblock | ftnprg
As an alternative to dd, you can call the getc(3F) library routine to read characters from the tape. You can then combine the characters into a character variable and use internal I/O to transfer formatted data. See also TOPEN(3F).
The end-of-file condition is reached when an end-of-file record is encountered during execution of a READ statement. The standard states that the file is positioned after the end-of-file record. In real life, this means that the tape read head is poised at the beginning of the next file on the tape. Although it seems as if you could read the next file on the tape, this is not strictly true, and is not covered by the ANSI FORTRAN 77 Language Standard.
The standard also says that a BACKSPACE or REWIND statement can be used to reposition the file. Consequently, after reaching end-of-file, you can backspace over the end-of-file record and further manipulate the file-for example, writing more records at the end, rewinding the file, and rereading or rewriting it.
The name used to open the tape file determines certain characteristics of the connection, such as the recording density and whether the tape is automatically rewound when opened and closed.
To access a file on a tape with multiple files, first use the mt(1) utility to position the tape to the needed file. Then open the file as a no-rewind magnetic tape such as /dev/nrmt0. Referencing the tape with this name prevents it from being repositioned when it is closed. By reading the file until end-of-file and then reopening it, a program can access the next file on the tape. Any program subsequently referencing the same tape can access it where it was last left, preferably at the beginning of a file, or past the end-of-file record.
However, if your program terminates prematurely, it may leave the tape positioned anywhere. Use the SunOS mt(1) command to reposition the tape appropriately.