HP Fortran for OpenVMS
Language Reference Manual


Previous Contents Index


Chapter 11
I/O Formatting

This chapter contains information on the following topics:

11.1 Overview

A format appearing in an input or output (I/O) statement specifies the form of data being transferred and the data conversion (editing) required to achieve that form. The format specified can be explicit or implicit.

Explicit format is indicated in a format specification that appears in a FORMAT statement or a character expression (the expression must evaluate to a valid format specification).

The format specification contains edit descriptors, which can be data edit descriptors, control edit descriptors, or string edit descriptors.

Implicit format is determined by the processor and is specified using list-directed or namelist formatting.

List-directed formatting is specified with an asterisk (*); namelist formatting is specified with a namelist group name.

List-directed formatting can be specified for advancing sequential files and internal files. Namelist formatting can be specified only for advancing sequential files.

For More Information:

11.2 Format Specifications

A format specification can appear in a FORMAT statement or character expression. In a FORMAT statement, it is preceded by the keyword FORMAT. A format specification takes the following form:

  • (format-list)

format-list

Is a list of one or more of the following edit descriptors, separated by commas or slashes (/):
Data edit descriptors: I, B, O, Z, F, E, EN, ES, D, G, L, and A.
Control edit descriptors: T, TL, TR, X, S, SP, SS, BN, BZ, P, :, /, $, \, and Q .
String edit descriptors: H, ' c ' , and "c", where c is a character constant.

A comma can be omitted in the following cases:

Edit descriptors can be nested and a repeat specification can precede data edit descriptors, the slash edit descriptor, or a parenthesized list of edit descriptors.

Rules and Behavior

A FORMAT statement must be labeled.

Named constants are not permitted in format specifications.

If the associated I/O statement contains an I/O list, the format specification must contain at least one data edit descriptor or the control edit descriptor Q.

Blank characters can precede the initial left parenthesis, and additional blanks can appear anywhere within the format specification. These blanks have no meaning unless they are within a character string edit descriptor.

When a formatted input statement is executed, the setting of the BLANK specifier (for the relevant logical unit) determines the interpretation of blanks within the specification. If the BN or BZ edit descriptors are specified for a formatted input statement, they supersede the default interpretation of blanks. (For more information on BLANK defaults, see Section 12.6.4.)

For formatted input, use the comma as an external field separator. The comma terminates the input of fields (for noncharacter data types) that are shorter than the number of characters expected. It can also designate null (zero-length) fields.

The first character of a record transmitted to a line printer or terminal is typically used for carriage control; it is not printed. The first character of such a record should be a blank, 0, 1, $, +, or ASCII NUL. Any other character is treated as a blank.

A format specification cannot specify more output characters than the external record can contain. For example, a line printer record cannot contain more than 133 characters, including the carriage control character.

Table 11-1 summarizes the edit descriptors that can be used in format specifications.

Table 11-1 Summary of Edit Descriptors
Code Form Effect  
A A[w] Transfers character or Hollerith values. (Section 11.3.6)
B Bw[.m] Transfers binary values. (Section 11.3.3.2)
BN BN Ignores embedded and trailing blanks in a numeric input field. (Section 11.4.4.1)
BZ BZ Treats embedded and trailing blanks in a numeric input field as zeros. (Section 11.4.4.2)
D Dw.d Transfers real values with D exponents. (Section 11.3.4.2)
E Ew.d[Ee] Transfers real values with E exponents. (Section 11.3.4.2)
EN ENw.d[Ee] Transfers real values with engineering notation. (Section 11.3.4.3)
ES ESw.d[Ee] Transfers real values with scientific notation. (Section 11.3.4.4)
F Fw.d Transfers real values with no exponent. (Section 11.3.4.1)
G Gw.d[Ee] Transfers values of all intrinsic types. (Section 11.3.4.5)
H nHch[ch...] Transfers characters following the H edit descriptor to an output record. (Section 11.5)
I Iw[.m] Transfers decimal integer values. (Section 11.3.3.1)
L Lw Transfers logical values: on input, transfers characters; on output, transfers T or F. (Section 11.3.5)
O Ow[.m] Transfers octal values. (Section 11.3.3.3)
P kP Interprets certain real numbers with a specified scale factor. (Section 11.4.5)
Q Q Returns the number of characters remaining in an input record. (Section 11.4.9)
S S Reinvokes optional plus sign (+) in numeric output fields; counters the action of SP and SS. (Section 11.4.3.3)
SP SP Writes optional plus sign (+) into numeric output fields. (Section 11.4.3.1)
SS SS Suppresses optional plus sign (+) in numeric output fields. (Section 11.4.3.2)
T Tn Tabs to specified position. (Section 11.4.2.1)
TL TLn Tabs left the specified number of positions. (Section 11.4.2.2)
TR TRn Tabs right the specified number of positions. (Section 11.4.2.3)
X nX Skips the specified number of positions. (Section 11.4.2.4)
Z Zw[.m] Transfers hexadecimal values. (Section 11.3.3.4)
$ $ Suppresses trailing carriage return during interactive I/O. (Section 11.4.8)
: : Terminates format control if there are no more items in the I/O list. (Section 11.4.7)
/ [r]/ Terminates the current record and moves to the next record. (Section 11.4.6)
\ \ Continues the same record; same as $. (Section 11.4.8)
' c ' 1 ' c ' Transfers the character literal constant (between the delimiters) to an output record. (Section 11.5)


1These delimiters can also be quotation marks (").

Character Format Specifications

In data transfer I/O statements, a format specifier ([FMT=]format) can be a character expression that is a character array, character array element, or character constant. This type of format is also called a run-time format because it can be constructed or altered during program execution.

The expression must evaluate to a character string whose leading part is a valid format specification (including the enclosing parentheses).

If the expression is a character array element, the format specification must be contained entirely within that element.

If the expression is a character array, the format specification can continue past the first element into subsequent consecutive elements.

If the expression is a character constant delimited by apostrophes, use two consecutive apostrophes ( '' ) to represent an apostrophe character in the format specification; for example:


PRINT '("NUM can''t be a real number")' 

Similarly, if the expression is a character constant delimited by quotation marks, use two consecutive quotation marks ("") to represent a quotation mark character in the format specification.

To avoid using consecutive apostrophes or quotation marks, you can put the character constant in an I/O list instead of a format specification, as follows:


PRINT "(A)", "NUM can't be a real number" 

The following shows another character format specification:


WRITE (6, '(I12, I4, I12)') I, J, K 

In the following example, the format specification changes with each iteration of the DO loop:


SUBROUTINE PRINT(TABLE) 
REAL TABLE(10,5) 
CHARACTER*5 FORCHR(0:5), RPAR*1, FBIG, FMED, FSML 
DATA FORCHR(0),RPAR /'(',')'/ 
DATA FBIG,FMED,FSML /'F8.2,','F9.4,','F9.6,'/ 
DO I=1,10 
  DO J=1,5 
    IF (TABLE(I,J) .GE. 100.) THEN 
      FORCHR(J) = FBIG 
    ELSE IF (TABLE(I,J) .GT. 0.1) THEN 
      FORCHR(J) = FMED 
    ELSE 
      FORCHR(J) = FSML 
    END IF 
  END DO 
  FORCHR(5)(5:5) = RPAR 
  WRITE (6,FORCHR) (TABLE(I,J), J=1,5) 
END DO 
END 

The DATA statement assigns a left parenthesis to character array element FORCHR(0), and (for later use) a right parenthesis and three F edit descriptors to character variables.

Next, the proper F edit descriptors are selected for inclusion in the format specification. The selection is based on the magnitude of the individual elements of array TABLE.

A right parenthesis is added to the format specification just before the WRITE statement uses it.

Note

Format specifications stored in arrays are recompiled at run time each time they are used. If a Hollerith or character run-time format is used in a READ statement to read data into the format itself, that data is not copied back into the original array, and the array is unavailable for subsequent use as a run-time format specification.

For More Information:

11.3 Data Edit Descriptors

A data edit descriptor causes the transfer or conversion of data to or from its internal representation.

The part of a record that is input or output and formatted with data edit descriptors (or character string edit descriptors) is called a field.

This section describes the forms for data edit descriptors and the individual descriptors, themselves. It also describes general rules for numeric editing and default widths for data edit descriptors.

11.3.1 Forms for Data Edit Descriptors

A data edit descriptor takes one of the following forms:

  • [r]c
  • [r]cw
  • [r]cw.m
  • [r]cw.d
  • [r]cw.d[Ee]

r

Is a repeat specification. The range of r is 1 through 2147483647 (2**31--1). If r is omitted, it is assumed to be 1.

c

Is one of the following format codes: I, B, O, Z, F, E, EN, ES, D, G, L, or A.

w

Is the total number of digits in the field (the field width). If omitted, the system applies default values (see Section 11.3.7). The range of w is 1 through 2147483647 (2**31--1). For I, B, O, Z, and F, the range can start at zero.

m

Is the minimum number of digits that must be in the field (including leading zeros). The range of m is 0 through 32767 (2**15--1).

d

Is the number of digits to the right of the decimal point (the significant digits). The range of d is 0 through 32767 (2**15--1).

The number of significant digits is affected if a scale factor is specified for the data edit descriptor.

E

Identifies an exponent field.

e

Is the number of digits in the exponent. The range of e is 1 through 32767 (2**15--1).

Rules and Behavior

Fortran 95/90 (and the previous standard) allows the field width to be omitted only for the A descriptor. However, HP Fortran allows the field width to be omitted for any data edit descriptor.

The r, w, m, d, and e must all be positive, unsigned, integer literal constants; or variable format expressions; no kind parameter can be specified. They must not be named constants.

Actual useful ranges for r, w, m, d, and e may be constrained by record sizes (RECL) and the file system.

The data edit descriptors have the following specific forms:
Integer: Iw[.m], Bw[.m], Ow[.m], and Zw[.m]
Real and complex: Fw.d, Ew.d[Ee], ENw.d[Ee], ESw.d[Ee], Dw.d, and Gw.d[Ee]
Logical: Lw
Character: A[w]

The d must be specified with F, E, D, and G field descriptors even if d is zero. The decimal point is also required. You must specify both w and d, or omit them both.

A repeat specification can simplify formatting. For example, the following two statements are equivalent:


20  FORMAT (E12.4,E12.4,E12.4,I5,I5,I5,I5) 
20  FORMAT (3E12.4,4I5) 

For More Information:

11.3.2 General Rules for Numeric Editing

The following rules apply to input and output data for numeric editing (data edit descriptors I, B, O, Z, F, E, EN, ES, D, and G).

Rules for Input Processing

Leading blanks in the external field are ignored. If BLANK= ' NULL ' is in effect (or the BN edit descriptor has been specified) embedded and trailing blanks are ignored; otherwise, they are treated as zeros. An all-blank field is treated as a value of zero.

The following table shows how blanks are interpreted by default:
Type of Unit or File Default
An explicitly OPENed unit BLANK= ' NULL '
An internal file BLANK= ' NULL '
A preconnected file 1 BLANK= ' NULL '


1For interactive input from preconnected files, you should explicitly specify the BN or BZ edit descriptor to ensure desired behavior.

A minus sign must precede a negative value in an external field; a plus sign is optional before a positive value.

In input records, constants can include any valid kind parameter. Named constants are not permitted.

If the data field in a record contains fewer than w characters, an input statement will read characters from the next data field in the record. You can prevent this by padding the short field with blanks or zeros, or by using commas to separate the input data. The comma terminates the data field, and can also be used to designate null (zero-length) fields. For more information, see Section 11.3.8.

Rules for Output Processing

The field width w must be large enough to include any leading plus or minus sign, and any decimal point or exponent. For example, the field width for an E data edit descriptor must be large enough to contain the following:

A positive or zero value (zero is allowed for I, B, O, Z, and F descriptors) can have a plus sign, depending on which sign edit descriptor is in effect. If a value is negative, the leftmost nonblank character is a minus sign.

If the value is smaller than the field width specified, leading blanks are inserted (the value is right-justified). If the value is too large for the field width specified, the entire output field is filled with asterisks (*).

When the value of the field width is zero, the compiler selects the smallest possible positive actual field width that does not result in the field being filled with asterisks.

For More Information:


Previous Next Contents Index