• Aucun résultat trouvé

Formatting FORTRAN Records-FORMAT Statement

Dans le document FORMAT (3(1) STOP (Page 118-124)

When you're using formatted I/O, the FORMAT statement lets you specify the format of the FORTRAN records in READ, WRITE, or REWRITE statements.

You can place FORMAT statements anywhere between the first and last statements in your program unit; you must specify a statement label.

You can use the FORMAT statement with both external and internal files.

When you use it for external files, be sure that the size of the FORMAT record doesn't exceed the size of the input/output medium; for example, if you're sending the record to a printer, it must not be longer than the printer line length. See

"Preface" on page iii for a list of publications containing device information.

When you use formatted output, be sure that the representation of the number does not exceed the width of the output field. If the width is exceeded, asterisks are written instead of data. For example, F7.3 cannot be used for any number that is less than -99.999 or greater than 999.999.

Each field in the FORTRAN record is described with a FORMAT code specifying the data type for the field. The order in which you specify the codes is their order in the record. Some of the codes available with VS FORTRAN are shown in Figure 24 on page 92 (for a comprehensive list, see VS FORTRAN Language and Library Reference).

Chapter 5. Programming Input and Output

91

FORMAT Code Meoning

Aw

aAw

pP,aEw.dEe alw.m

aGw.dEe

/

HN

HZ

s

Data Field Codes:

Character data field (optional length specification)

Character data field (optional repeat count)

Real data field (optional exponent (Ee»

Integer data field (with minimum number of digits to be displayed (.m»

mM Extension

Integer, real, or logical data field (optional exponent (Ee»

L

End of IBM Extension _ _ _ _ _ _ _ _ _ _ _ _ --' Edit Codes:

End of format control, but only if I/O list is completely processed

End of record

N onleading blanks in a numeric field are ignored on input

Blanks treated as zeros on input Specifies display of an optional plus sign

Figure 24 (Part 1 of 2). Some Codes Used with the FORMAT Statement

SP

ss

TLr

TRr

Specifies plus sign must be produced on output

Specifies plus sign is not to be produced on output

Data transfer starts r characters to left

Data transfer starts r characters to right

The lowercase letters have the following meanings:

a An optional repeat count, less than 256 d The number of decimal places to be carried e The number of digits in the exponent field m The minimum number of digits to be displayed p The number of digits for the scale factor r A character displacement in a record

w The total number of characters in a field

Figure 24 (Part 2 of 2). Some Codes Used with the FORMAT Statement

Example:

If you want to define the format of an output record, you could specify your FORMAT statement as follows:

200 FORMAT (SP,2A10,I6.4,2E14.5E2)

which specifies that the output line is to be formatted as follows:

SP specifies that if the value of any of the. numeric fields is positive, a plus sign is to be displayed. (If the value is negative, a minus sign is always displayed. )

2AIO specifies that the first and second items are character items of length 10.

16.4 specifies that the third item is an integer item of total length 6, and that when the line is produced the display is:

(blank) (+ or -) (4 digits)

2E14.SE2 specifies that the fourth and fifth items are real items of total length 14; the display for each is shown in Figure 25 on page 94.

Chapter 5. Programming Input and Output

93

(2 blanks) (sign) (0) (decimal point) (5 digits) (E) (sign) (2 digits)

I II I

Numeric Field Exponent

(The sign is displayed as a + or a -.) Figure 25. Display for FORMAT E14.5E2

Notes to Figure 2S :

1. The total width you can specify for this field is 14: 2 characters for leading blanks, 7 characters for the numeric field (including the leading sign and the decimal point), and 4 characters for the exponent (including the E and the sign).

2. The length of the record you've defined is 54 characters (bytes).

A formatted WRITE statement uses the statement label for this FORMAT statement and writes a record in this format.

Group FORMAT Specifications

VS FORTRAN lets you specify group specifications nested within the overall FORMAT specification, by specifying the group within parentheses. The group can contain a combination of format codes and groups, each separated by commas, slashes, or colons.

For example, you could specify an input record as follows:

100 FORMAT (BZ,A4,(A8,(I4,E8.4,(E4.0,E8.2»»

which specifies that the input receiving fields are structured as follows:

• BZ specifies that blanks in the input are treated as zeros.

• The first field in the record, A4, is a character field of length 4.

• It is followed by a group field, (A8,(I4,E8.4,(E4.0,E8.2»), consisting of the following:

An 8-character field, A8, followed by A nested group field, (I4,E8.4,(E4.0,E8.2».

• This nested group field contains yet another nested group field, (E4.0,E8.2).

Note: Up to 50 levels for nesting group fields can be specified in the VS FORTRAN FORMAT statement.

Using Specifications Repeatedly-FORMAT Control

Your FORMAT statements need not contain a format specification for each field in the READ or WRITE I/O list. If the end of the FORMAT specification list is reached before the last item in the I/O list is processed, control is returned to the rightmost left parenthesis in the format list; if there aren't any embedded

parentheses, then control is returned to the first item in the format list:

10 FORMAT (A4,2(I2,I4),3(I4,I4),E8.2)

In this example, control returns to the repeat count in 3(14,14). A new record is processed each time control returns to the repeat count.

You can take advantage of this to reduce your coding effort, but be sure that the items repeated are the items you want repeated.

Using One FORMAT Statement with Variable Formats

Internal I/O

You can specify variable FORMAT statements by placing a format specification into a character variable or an array during execution. You could read the specification in from external storage, or you could initialize the area using a DATA statement or an explicit assignment statement. You can then use the array as the format specification in READ or WRITE statements.

Using this feature, you can refer to a different array element each time you execute the READ or WRITE statement, and thus change the format.

Internal I/O statements let you move data from one internal storage area to another while converting it from one format to another. This gives you a convenient and standard method of making such conversions.

If your external file is coded in EBCDIC character data, you can execute an unformatted READ statement to·bring it into a character item in storage:

You can then execute a formatted internal READ statement to convert individual items in the record from EBCDIC to their internal formats.

For internal READ and WRITE statements, you specify the UNIT identifier as an internal data item-a character variable or a substring, or as a character array or an array element.

Chapter 5. Programming Input and Output

95

Using the READ Statement-Internal Files

A READ statement referring to an internal unit converts the data from character format to the internal format(s) of the receiving item(s):

CHARACTER*13 BUF CHARACTER*3 CH

BUF = '5ABC 2.0'

READ ( UNIT= BUF, FMT=40) I , CH, A 40 FORMAT (12, lX, A3, 2X, F5.0)

Execution of the READ statement in this case is equivalent to executing the following assignment statements:

1=5 CH = 'ABC' and A = 2.0 Using the WRITE Statement-Internal Files

NAMELIST I/O

A WRITE statement referring to an internal unit converts the data transferred from internal format to character format:

CHARACTER *13 BUF CHARACTER * 3 CH 1=5

A = 2.0 CH = 'ABC'

WRITE ( UNIT=BUF, FMT=40) I, CH, A 40 FORMAT (12, lX, A3, 2X, F5.1)

Execution of the WRITE statement in this case is equivalent to executing the following assignment statement:

BUF = ' 5 ABC 2.0'

The NAMELIST statement specifies one or more lists of names of variables or arrays for use in READ and WRITE statements.

You can also use a READ statement to transfer data from an external 110 device into storage, or from one area of internal storage to one or more other areas of internal storage. In both cases, you must specify the NAMELIST statement to associate the name given to the data in the FORTRAN program with the data itself.

For more details and examples, see VS FORTRAN Language and Library Reference.

mM Extension

Dans le document FORMAT (3(1) STOP (Page 118-124)