Format Specifications (2024)

Format Specifications (1) Format Specifications (2) Format Specifications (3) Format Specifications (4) Format Specifications (5)
Next: FORMAT statement Up: Input/Output Facilities Previous: End-of-file Detection

Every READ or WRITE statement which uses a formatted external file or an internal file must include a format identifier. This may have any of the following forms:

FMT=*
This specifies a list-directed transfer (and is only permitted for external sequential files). Detailed rules are given in section 10.10 below.
FMT=label
The label must be attached to a FORMAT statement in the same program unit which provides the format specification.
FMT=char-exp
The value of the character expression is a complete format specification.
FMT=char-array
The elements of the character arraycontain the format specification, which may occupy as many elements of the array as are necessary.
Note that the characters FMT= may be omitted if it is the second item in the I/O control list and if the unit identifier with UNIT= omitted comes first.

A format specification consists a pair of parentheses enclosing a list of items called edit descriptors. Any blanks before the left parenthesis will be ignored and (except in a FORMAT statement) all characters after the matching right parenthesis are ignored.

In most cases the format can be chosen when the program is written and the simplest option is to use a character constant:
WRITE(UNIT=LP, FMT='(1X,A,F10.5)') 'Frequency =', HERTZ
Alternatively you can use a FORMAT statement:

 WRITE(UNIT=LP, FMT=915) 'Frequency =', HERTZ 915 FORMAT(1X, A, F10.5)
This allows the same format to be used by more than one data-transferstatement. The FORMAT statement may also be the neater form if thespecification is long and complicated, or if character-constantdescriptors are involved, since the enclosing apostrophes have to bedoubled up if the whole format is part of another character constant.

It is also possible to compute a format specification at run-time by using a suitable character expression. By this means you could, for example, arrange to read the format specification of a data file from the first record of the file. The program fragment below shows how to output a real number in fixed-point format (F10.2) when it is small, changing to exponential format (E18.6) when it is larger. A threshold of a million has been chosen here.

 CHARACTER F1*(*), F2*12, F3*(*) *Items F1, F2, F3 hold the three parts of a format specification. *F1 and F3 are constants, F2 is a variable. PARAMETER (F1 = '(1X,''Peak size ='',') PARAMETER (F3 = ')') *... calculation of PEAK assumed to be in here IF(PEAK .LT. 1.0E6) THEN F2 = 'F10.2' ELSE F2 = 'E18.6' END IF WRITE(UNIT=*, FMT=F1//F2//F3) PEAK
Note that the apostrophes surrounding the character constant 'Peak size =' have been doubled in the PARAMETER statement because they are inside another character constant. Here are two examples of output records, with blanks shown explicitly:
 Peak size = 12345.67 Peak size = 0.987654E+08
Format Specifications (6) Format Specifications (7) Format Specifications (8) Format Specifications (9) Format Specifications (10)
Next: FORMAT statement Up: Input/Output Facilities Previous: End-of-file Detection
Helen Rowlands
8/27/1998
Format Specifications (2024)
Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 6140

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.