For the syntax of <format and list part>, see READ Statement. The format and list part element indicates which variables contain the data and how the data is to be interpreted.
If the format and list part element is omitted in a WRITE statement, a logically empty record is written. The actual output is device-dependent. Printers interpret this as a blank record; disks and tapes interpret this as a record with undefined contents.
Formatted Write
A WRITE statement that contains a format designator, editing specifications, or a free-field part is called a formatted write.
A format designator without a list indicates that the referenced format contains one or more string literals that constitute the entire output of the WRITE statement.
A format designator with a list indicates that the variables in the list are to be written in the format described by the referenced format.
Editing specifications can appear in place of a format designator and have the same effect as if they had been declared in a FORMAT declaration and had been referenced through a format designator. For more information, refer to FORMAT Declaration.
Binary Write
A WRITE statement of the following form is called a binary write:
WRITE(<write file part>,*,<list>)
A binary WRITE statement can be used to write variables to a file in an internal form that can later be read with a binary READ statement.
An asterisk (*) followed by a list specifies that the elements in the list are to be processed as full words and are to be written without being edited. The number of words written is determined by the number of elements in the list or the maximum record size, whichever is smaller. When unblocked records are used, the block size is the maximum record size.
In a binary write, when the record number or carriage control element is LINE, SKIP, STATION, TIMELIMIT, or STOP, it is ignored and treated as a serial binary write. If the carriage control element is SPACE, the number of lines specified plus one is spaced after writing the current record. The extra space occurs because in a binary write normal carriage control is performed first and then any extra spacing is done.
When writing a character array, only full words are written. If there is a partial word left at the end of the array, it is ignored. For example, if A is an EBCDIC array that contains the characters 12345678, the following statement writes only the characters 123456:
WRITE(FILEID,*,A)
When a string variable occurs in the list of a binary WRITE statement, a word containing the string length is written to the file before the contents of the string are written. This feature allows the program to write string information that can later be read through a binary READ statement. For more information, see “Binary Read” under READ Statement.
Array Row Write
A WRITE statement of any of the following forms is called an array row write:
WRITE(<write file part>,<arithmetic expression>,<array row>)
WRITE(<write file part>,<arithmetic expression>,
<subscripted variable>)
WRITE(<write file part>,<arithmetic expression>,
<pointer expression>)
WRITE(<write file part>,<arithmetic expression>,<string variable>)The first three forms of the array row write specify that the elements of the designated array row, subscripted variable, or item referenced by the pointer expression are to be processed as full words and are to be written without being edited. The number of words written is determined by the smallest of the following:
-
The number of elements in the array row, subscripted variable, or item referenced by the pointer expression
-
The maximum record length
-
The greater of the absolute value of the arithmetic expression or one (1)
If the FILETYPE attribute of the file has a value of 6, then the maximum record length is ignored and records span block boundaries. When unblocked records are used, the block size is the maximum record size. If the UNITS attribute equals CHARACTERS and the INTMODE attribute does not equal SINGLE, then all counts represent characters, not words.
A WRITE statement of the following form specifies that the characters in the string variable are to be written without being edited:
WRITE(<write file part>,<arithmetic expression>,<string variable>)
The number of characters written is determined by the maximum record size, the absolute value of the arithmetic expression, or the length of the string, whichever is smallest. If the UNITS attribute equals CHARACTERS and the INTMODE attribute does not equal SINGLE, then all counts represent characters, not words.
A WRITE statement of the following form is not an array row write:
WRITE (<write file part>,<arithmetic expression>,<string literal>)
This kind of statement can have unexpected results because the number of words or characters written is determined by the arithmetic expression and the UNITS attribute of the file part. If the arithmetic expression in the units of the file is greater than the length of the string literal, unexpected output can occur. Use a formatted write of the following form to write a string literal:
WRITE(<file part>,<" <string literal> ">)
Free-Field Part
The free-field part allows output to be performed with editing but without using editing specifications. The appropriate format is selected automatically, but variations of the free-field part give the programmer some control over the form of the output.
On output, each value is edited into an appropriate format. An edited item is never split across a record boundary. If the record is too short to hold the representation of the item, a string of pound signs (#) is written in place of the item.
When a complex expression appears in the list of a free-field WRITE statement, two values are written. The first value corresponds to the real part, and the second value corresponds to the imaginary part.
Data items are normally separated by a comma and a space (,). If the free-field part contains two slashes, data items are separated by two spaces.
If the optional asterisk (*) is used, the name of the data item and an equal sign (=) are written to the left of the value of the data item. If the data item is not a variable, then the expression is written as the name of the data item.
If the free-field part includes the <number of columns> and <column width> elements, each list element is written in a separate column. This process is controlled by two column factors: the number of columns per record (r) and the width of each column (w), where w is measured in characters. Both r and w are integerized, if necessary.
If r is 0 (zero), the number of columns per record is determined from the value of w and the record length. If w is 0, the width of each column is determined from the value of r and the record length. If both r and w are 0, the output has no column structure. If r and w are such that r columns of w characters cannot fit on one record, adjustments are made to both r and w. The width of a column does not include the two-character delimiter; therefore, r*(w+2) must be less than or equal to the length of the record.
Example of a Free-Field Part
BEGIN FILE DCOM(KIND=REMOTE,MAXRECSIZE=12,MYUSE=IO); INTEGER I; REAL R; DOUBLE D; STRING S; I := 25; R := 1002459; D := 255; S := "string"; WRITE(DCOM,*/, I, R, D, S, I+R, R-D, S!!" ABCDE", 7.2); END.
The following output results when this program is executed:
I=25, R=1002459.0, D=2.5D+6, S=string, I+R=1002484.0, R-D=-1497541.0, S!!" ABCDE"=string ABCDE, <CNST>=7.2,
Additional information about I/O operations can be found under I/O Statement and READ Statement.

