next up previous contents index
Next: Unformatted Sequential Files Up: External Files Previous: External Files

Formatted Sequential Files

These are often just called text files . Terminals and printers should always be treated as formatted sequential files. Data files of this type can be created in a variety of ways, for example by direct entry from the keyboard, or by using a text editor. Some Fortran systems do not allow records to be longer than a normal line of text, for example 132 characters. Unless a text file is pre-connected it must be opened with an OPEN statement, but the FORM= and ACCESS= keywords are not needed as the default values are suitable:
All data transfers must be carried out under format control. There are two options with files of this type: you can either provide your own format specification or use list-directed formatting.

The attraction of list-directed I/O   is that the Fortran system does the work, providing simple data transfers with little programming effort. They are specified by having an asterisk as the format identifier:

      WRITE(UNIT=*, FMT=*)'Enter velocity: ' 
      READ(UNIT=*, FMT=*, END=999) SPEED
List-directed input is quite convenient when reading numbers from a terminal since it allows virtually ``free-format" data entry. It may also be useful when reading data files where the layout is not regular enough to be handled by a format specification. List-directed output is satisfactory when used just to output a character string (as in the example above), but it produces less pleasing results when used to output numerical values since you have no control over the positioning of items on the line, the field-width, or the number of decimal digits displayed. Thus:
      WRITE(UNIT=LP, FMT=*)' Box of',N,' costs ',PRICE
will produce a record something like this:
Box of 12 costs 9.5000000
List-directed output is best avoided except to write simple messages and for diagnostic output during program development. The rules for list-directed formatting are covered in detail in section 10.10.

The alternative is to provide a format specification: this provides complete control over the data transfer. The previous example can be modified to use a format specification like this:

      WRITE(UNIT=LP, FMT=55)'Box of',N,' costs ',PRICE 
55    FORMAT(1X, A, I3, A, F6.2)
and will produce a record like this:
Box of 12 costs 9.50
The format specification is provided in this case by a FORMAT statement: its label is the format identifier in the WRITE statement. Other ways of providing format specifications are described in section 10.6.

One unusual feature of input under control of a format specification is that each line of text will appear to be padded out on the right with an indefinite number of blanks irrespective of the actual length of the data record. This means that, among other things, it is not possible to distinguish between an empty record and one filled with blanks. If numbers are read from an empty record they will simply be zero.

next up previous contents index
Next: Unformatted Sequential Files Up: External Files Previous: External Files
Helen Rowlands