5.  FILE FORMATS

5.1.  Structure of Fortran Files

      Fortran requires four kinds of external files: sequential formatted and unformatted, and direct formatted and unformatted. On UNIXsystems, these are all implemented as ordinary files which are assumed to have the proper internal structure.

      Fortran I/O is based on ``records''. When a direct file is opened in a Fortran program, the record length of the records must be given, and this is used by the Fortran I/O system to make the file look as if it is made up of records of the given length. In the special case that the record length is given as 1, the files are not considered to be divided into records, but are treated as byte-addressable byte strings; that is, as ordinary UNIXfile system files. (A read or write request on such a file keeps consuming bytes until satisfied, rather than being restricted to a single record.)

      The peculiar requirements on sequential unformatted files make it unlikely that they will ever be read or written by any means except Fortran I/O statements. Each record is preceded and followed by an integer containing the record's length in bytes.

      The Fortran I/O system breaks sequential formatted files into records while reading by using each newline as a record separator. The result of reading off the end of a record is undefined according to the Standard. The I/O system is permissive and treats the record as being extended by blanks. On output, the I/O system will write a newline at the end of each record. It is also possible for programs to write newlines for themselves. This is an error, but the only effect will be that the single record the user thought he wrote will be treated as more than one record when being read or backspaced over.

5.2.  Portability Considerations

      The Fortran I/O system uses only the facilities of the standard C I/O library, a widely available and fairly portable package, with the following two nonstandard features: The I/O system needs to know whether a file can be used for direct I/O, and whether or not it is possible to backspace. Both of these facilities are implemented using the fseek routine, so there is a routine canseek which determines if fseek will have the desired effect. Also, the inquire statement provides the user with the ability to find out if two files are the same, and to get the name of an already opened file in a form which would enable the program to reopen it. (The UNIXoperating system implementation attempts to determine the full pathname.) Therefore there are two routines which depend on facilities of the operating system to provide these two services. In any case, the I/O system runs on the PDP-11, VAX-11/780, and Interdata 8/32 UNIXsystems.

5.3.  Pre-Connected Files and File Positions

      Units 5, 6, and 0 are preconnected when the program starts. Unit 5 is connected to the standard input, unit 6 is connected to the standard output, and unit 0 is connected to the standard error unit. All are connected for sequential formatted I/O.

      All the other units are also preconnected when execution begins. Unit n is connected to a file named fort.n. These files need not exist, nor will they be created unless their units are used without first executing an open. The default connection is for sequential formatted I/O.

      The Standard does not specify where a file which has been explicitly opened for sequential I/O is initially positioned. In fact, the I/O system attempts to position the file at the end, so a write will append to the file and a read will result in an end-of-file indication. To position a file to its beginning, use a rewind statement. The preconnected units 0, 5, and 6 are positioned as they come from the program's parent process.

REFERENCES

1.
Sigplan Notices 11, No.3 (1976), as amended in X3J3 internal documents through ``/90.1''.
2.
USA Standard FORTRAN, USAS X3.9-1966, New York: United States of America Standards Institute, March 7, 1966. Clarified in Comm. ACM 12, 289 (1969) and Comm. ACM 14, 628 (1971).
3.
B. W. Kernighan and D. M. Ritchie, The C Programming Language, Englewood Cliffs: Prentice-Hall (1978).
4.
D. M. Ritchie, private communication.
5.
S. C. Johnson, ``A Portable Compiler: Theory and Practice'', Proc. 5th ACM Symp. on Principles of Programming Languages (January 1978).
6.
S. I. Feldman, ``An Informal Description of EFL'', internal memorandum.
7.
B. W. Kernighan, ``RATFOR -- A Preprocessor for a Rational Fortran'', Bell Laboratories Computing Science Technical Report #55, (January 1977).
8.
D. M. Ritchie, private communication.

APPENDIX. Differences Between Fortran 66 and Fortran 77

      The following is a very brief description of the differences between the 1966 [2] and the 1977 [1] Standard languages. We assume that the reader is familiar with Fortran 66. We do not pretend to be complete, precise, or unbiased, but plan to describe what we feel are the most important aspects of the new language. At present the only current information on the 1977 Standard is in publications of the X3J3 Subcommittee of the American National Standards Institute. The following information is from the ``/92'' document. This draft Standard is written in English rather than a meta-language, but it is forbidding and legalistic. No tutorials or textbooks are available yet.