[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP Fortran for OpenVMS
Language Reference Manual


Previous Contents Index

10.3.1.4 Rules for Unformatted Sequential READ Statements

Unformatted, sequential READ statements transfer binary data (without translation) between the current record and the entities specified in the I/O list. Only one record is read.

Objects of intrinsic or derived types can be transferred.

For data transfer, the file must be positioned so that the record read is an unformatted record or an end-of-file record.

The unformatted, sequential READ statement reads a single record. Each value in the record must be of the same type as the corresponding entity in the input list, unless the value is real or complex.

If the value is real or complex, one complex value can correspond to two real list entities, or two real values can correspond to one complex list entity. The corresponding values and entities must have the same kind parameter.

If the number of I/O list items is less than the number of fields in an input record, the statement ignores the excess fields. If the number of I/O list items is greater than the number of fields in an input record, an error occurs.

If a statement contains no I/O list, it skips over one full record, positioning the file to read the following record on the next execution of a READ statement.

If the file is connected for formatted, list-directed, or namelist I/O, unformatted data transfer is prohibited.

Examples

The following example shows an unformatted, sequential READ statement:


READ (UNIT=6, IOSTAT=IO_STATUS) A, B, C

10.3.2 Forms for Direct-Access READ Statements

Direct-access READ statements transfer input data from external records with direct access. (The attributes of a direct-access file are established by the OPEN statement.)

A direct-access READ statement can be formatted or unformatted, and takes one of the following forms:

Formatted


  • READ (eunit, format, rec [,iostat] [,err]) [io-list]

Unformatted


  • READ (eunit, rec [,iostat] [,err]) [io-list]

eunit

Is an external unit specifier ([UNIT=]io-unit).

format

Is a format specifier ([FMT=]format). It must not be an asterisk (*).

rec

Is a record specifier (REC=r).

iostat

Is a status specifier (IOSTAT=i-var).

err

Is a branch specifier (ERR=label) if an error condition occurs.

io-list

Is an I/O list.

For More Information:

  • On I/O control-list specifiers, see Section 10.2.1.
  • On I/O lists, see Section 10.2.2.
  • On file sharing, see the HP Fortran for OpenVMS User Manual.

10.3.2.1 Rules for Formatted Direct-Access READ Statements

Formatted, direct-access READ statements translate data from character to binary form by using format specifications for editing (if any). The translated data is assigned to the entities in the I/O list in the order in which the entities appear, from left to right.

Values can be transferred to objects of intrinsic or derived types. For derived types, values of intrinsic types are transferred to the components of intrinsic types that ultimately make up these structured objects.

For data transfer, the file must be positioned so that the record read is a formatted record or an end-of-file record.

If the number of I/O list items is less than the number of fields in an input record, the statement ignores the excess fields.

If the number of I/O list items is greater than the number of fields in an input record, the input record is padded with blanks. However, if PAD= ' NO ' was specified for file connection, the input list and file specification must not require more characters from the record than it contains. If more characters are required and nonadvancing input is in effect, an end-of-record condition occurs.

If the format specification specifies another record, the record number is increased by one as each subsequent record is read by that input statement.

Examples

The following example shows a formatted, direct-access READ statement:


READ (2, REC=35, FMT=10) (NUM(K), K=1,10)

10.3.2.2 Rules for Unformatted Direct-Access READ Statements

Unformatted, direct-access READ statements transfer binary data (without translation) between the current record and the entities specified in the I/O list. Only one record is read.

Objects of intrinsic or derived types can be transferred.

For data transfer, the file must be positioned so that the record read is an unformatted record or an end-of-file record.

The unformatted, direct-access READ statement reads a single record. Each value in the record must be of the same type as the corresponding entity in the input list, unless the value is real or complex.

If the value is real or complex, one complex value can correspond to two real list entities, or two real values can correspond to one complex list entity. The corresponding values and entities must have the same kind parameter.

If the number of I/O list items is less than the number of fields in an input record, the statement ignores the excess fields. If the number of I/O list items is greater than the number of fields in an input record, an error occurs.

If the file is connected for formatted, list-directed, or namelist I/O, unformatted data transfer is prohibited.

Examples

The following example shows unformatted, direct-access READ statements:


READ (1, REC=10) LIST(1), LIST(8)
READ (4, REC=58, IOSTAT=K, ERR=500) (RHO(N), N=1,5)

10.3.3 Forms for Indexed READ Statements

Indexed READ statements transfer input data from external records that have keyed access.

In an indexed file, a series of records can be read in key value sequence by using an indexed READ statement and sequential READ statements. The first record in the sequence is read by using the indexed statement, the rest are read by using the sequential READ statements.

An indexed READ statement can be formatted or unformatted, and takes one of the following forms:

Formatted


  • READ (eunit, format, key [,keyid] [,iostat] [,err]) [io-list]

Unformatted


  • READ (eunit, key [,keyid] [,iostat] [,err]) [io-list]

eunit

Is an external unit specifier ([UNIT=]io-unit).

format

Is a format specifier ([FMT=]format).

key

Is a key specifier (KEY[con]=value).

keyid

Is a key-of-reference specifier (KEYID=kn).

iostat

Is a status specifier (IOSTAT=i-var).

err

Is a branch specifier (ERR=label) if an error condition occurs.

io-list

Is an I/O list.

For More Information:

10.3.3.1 Rules for Formatted Indexed READ Statements

Formatted, indexed READ statements translate data from character to binary form by using format specifications for editing (if any). The translated data is assigned to the entities in the I/O list in the order in which the entities appear, from left to right.

If the I/O list and format specifications indicate that additional records are to be read, the statement reads the additional records sequentially by using the current key-of-reference value.

If KEYID is omitted, the key-of-reference value is the same as the most recent specification. If KEYID is omitted from the first indexed READ statement, the key of reference is the primary key.

If the specified key value is shorter than the key field referenced, the key value is matched against the leftmost characters of the appropriate key field until a match is found. The record supplying the match is then read. If the key value is longer than the key field referenced, an error occurs.

If the file is connected for unformatted I/O, formatted data transfer is prohibited.

Examples

Suppose the following statement is specified:


READ (3, KAT(25), KEY='ABCD') A,B,C,D

The READ statement retrieves a record with a key value of ' ABCD ' in the primary key from the file connected to I/O unit 3. It then uses the format contained in the array item KAT(25) to read the first four fields from the record into variables A,B,C, and D.

10.3.3.2 Rules for Unformatted Indexed READ Statements

Unformatted, indexed READ statements transfer binary data (without translation) between the current record and the entities specified in the I/O list. Only one record is read.

If the number of I/O list items is less than the number of fields in the record being read, the unused fields in the record are discarded. If the number of I/O list items is greater than the number of fields, an error occurs.

If a specified key value is shorter than the key field referenced, the key value is matched against the leftmost characters of the appropriate key field until a match is found. The record supplying the match is then read. If the specified key value is longer than the key field referenced, an error occurs.

If the file is connected for formatted I/O, unformatted data transfer is prohibited.

Examples

Suppose the following statements are specified:


      OPEN (UNIT=3, STATUS='OLD',
1     ACCESS='KEYED', ORGANIZATION='INDEXED',
2     FORM='UNFORMATTED',
3     KEY=(1:5,30:37,18:23))
      READ (3,KEY='SMITH') ALPHA, BETA

The READ statement reads from the file connected to I/O unit 3 and retrieves the record with the value ' SMITH ' in the primary key field (bytes 1 through 5). The first two fields of the record retrieved are placed in variables ALPHA and BETA, respectively.

Suppose the following statement is specified:


READ (3,KEYGE='XYZDEF',KEYID=2,ERR=99) IKEY

In this case, the READ statement retrieves the first record having a value equal to or greater than ' XYZDEF ' in the second alternate key field (bytes 18 through 23). The first field of that record is placed in variable IKEY.

10.3.4 Forms and Rules for Internal READ Statements

Internal READ statements transfer input data from an internal file.

An internal READ statement can only be formatted. It must include format specifiers (which can use list-directed formatting). Namelist formatting is not permitted.

An internal READ statement takes the following form:


  • READ (iunit, format [,iostat] [,err] [,end]) [io-list]

iunit

Is an internal unit specifier ([UNIT=]io-unit). It must be a character variable. It must not be an array section with a vector subscript.

format

Is a format specifier ([FMT=]format). An asterisk (*) indicates list-directed formatting.

iostat

Is a status specifier (IOSTAT=i-var).

err, end

Are branch specifiers if an error (ERR=label) or end-of-file (END=label) condition occurs.

io-list

Is an I/O list.

Formatted, internal READ statements translate data from character to binary form by using format specifications for editing (if any). The translated data is assigned to the entities in the I/O list in the order in which the entities appear, from left to right.

This form of READ statement behaves as if the format begins with a BN edit descriptor. (You can override this behavior by explicitly specifying the BZ edit descriptor.)

Values can be transferred to objects of intrinsic or derived types. For derived types, values of intrinsic types are transferred to the components of intrinsic types that ultimately make up these structured objects.

Before data transfer occurs, the file is positioned at the beginning of the first record. This record becomes the current record.

If the number of I/O list items is less than the number of fields in an input record, the statement ignores the excess fields.

If the number of I/O list items is greater than the number of fields in an input record, the input record is padded with blanks. However, if PAD= ' NO ' was specified for file connection, the input list and file specification must not require more characters from the record than it contains.

In list-directed formatting, character strings have no delimiters.

Examples

An internal read can be used to convert character data to numeric values.

The following program segment reads a record and examines the first character to determine whether the remaining data should be interpreted as decimal, octal, or hexadecimal. It then uses internal READ statements to make appropriate conversions from character string representations to binary.


INTEGER IVAL
CHARACTER TYPE, RECORD*80
CHARACTER*(*) AFMT, IFMT, OFMT, ZFMT
PARAMETER (AFMT='(Q,A)', IFMT= '(I10)', OFMT= '(O11)',        &
           ZFMT= '(Z8)')
ACCEPT AFMT, ILEN, RECORD
TYPE = RECORD(1:1)
IF (TYPE .EQ. 'D') THEN
    READ (RECORD(2:MIN(ILEN, 11)), IFMT) IVAL
ELSE IF (TYPE .EQ. 'O') THEN
    READ (RECORD(2:MIN(ILEN, 12)), OFMT) IVAL
ELSE IF (TYPE .EQ. 'X') THEN
    READ (RECORD(2:MIN(ILEN, 9)),ZFMT) IVAL
ELSE
    PRINT *, 'ERROR'
END IF
END

For More Information:

10.4 ACCEPT Statement

The ACCEPT statement is a data transfer input statement. This statement is the same as a formatted, sequential READ statement, except that an ACCEPT statement must never be connected to user-specified I/O units.

An ACCEPT statement takes one of the following forms:

Formatted


  • ACCEPT form [,io-list]

Formatted: List-Directed


  • ACCEPT * [,io-list]

Formatted: Namelist


  • ACCEPT nml

form

Is the nonkeyword form of a format specifier (no FMT=).

io-list

Is an I/O list.

*

Is the format specifier indicating list-directed formatting. (It can also be specified as FMT=*.)

nml

Is the nonkeyword form of a namelist specifier (no NML=) indicating namelist formatting.

Examples

In the following example, character data is read from the implicit unit and binary values are assigned to each of the five elements of array CHARAR:


      CHARACTER*10 CHARAR(5)
      ACCEPT 200, CHARAR
200   FORMAT (5A10)

For More Information:

10.5 WRITE Statements

The WRITE statement is a data transfer output statement. Data can be output to external sequential, keyed-access, or direct-access records, or to internal records.

10.5.1 Forms for Sequential WRITE Statements

Sequential WRITE statements transfer output data to external sequential access records. The statements can be formatted by using format specifiers (which can use list-directed formatting) or namelist specifiers (for namelist formatting), or they can be unformatted.

A sequential WRITE statement takes one of the following forms:

Formatted


  • WRITE (eunit, format [,advance] [,iostat] [,err]) [io-list]

Formatted: List-Directed


  • WRITE (eunit, * [,iostat] [,err]) [io-list]

Formatted: Namelist


  • WRITE (eunit, nml-group [,iostat] [,err])

Unformatted


  • WRITE (eunit [,iostat] [,err]) [io-list]

eunit

Is an external unit specifier ([UNIT=]io-unit).

format

Is a format specifier ([FMT=]format).

advance

Is an advance specifier (ADVANCE=c-expr). If the value of c-expr is ' YES ' , the statement uses advancing output; if the value is ' NO ' , the statement uses nonadvancing output. The default value is ' YES ' .

iostat

Is a status specifier (IOSTAT=i-var).

err

Is a branch specifier (ERR=label) if an error condition occurs.

io-list

Is an I/O list.

*

Is the format specifier indicating list-directed formatting. (It can also be specified as FMT=*.)

nml-group

Is a namelist specifier ([NML=]group) indicating namelist formatting.

For More Information:

10.5.1.1 Rules for Formatted Sequential WRITE Statements

Formatted, sequential WRITE statements translate data from binary to character form by using format specifications for editing (if any). The translated data is written to an external file that is connected for sequential access.

Values can be transferred from objects of intrinsic or derived types. For derived types, values of intrinsic types are transferred from the components of intrinsic types that ultimately make up these structured objects.

The output list and format specification must not specify more characters for a record than the record size. (Record size is specified by RECL in an OPEN statement.)

If the file is connected for unformatted I/O, formatted data transfer is prohibited.

Examples

The following example shows formatted, sequential WRITE statements:


WRITE (UNIT=8, FMT='(B)', ADVANCE='NO') C

WRITE (*, "(F6.5)", ERR=25, IOSTAT=IO_STATUS) A, B, C

10.5.1.2 Rules for List-Directed Sequential WRITE Statements

List-directed, sequential WRITE statements transfer data from binary to character form by using the data types of the corresponding I/O list item to determine the form of the data. The translated data is then written to an external file.

In general, values transferred as output have the same forms as values transferred as input.

Table 10-1 shows the default output formats for each intrinsic data type.

Table 10-1 Default Formats for List-Directed Output
Data Type Output Format
BYTE I5
LOGICAL(1) L2
LOGICAL(2) L2
LOGICAL(4) L2
LOGICAL(8) L2
INTEGER(1) I5
INTEGER(2) I7
INTEGER(4) I12
INTEGER(8) I22
REAL(4) 1PG15.7E2
REAL(8) T_floating 1PG24.15E3
REAL(8) D_floating 1PG24.16E2
REAL(8) G_floating 1PG24.15E3
REAL(16) 1PG43.33E4
COMPLEX(4) ' ( ' ,1PG14.7E2, ' , ' ,1PG14.7E2, ' ) '
COMPLEX(8) T_floating ' ( ' ,1PG23.15E3, ' , ' ,1PG23.15E3, ' ) '
COMPLEX(8) D_floating ' ( ' ,1PG23.16E2, ' , ' ,1PG23.16E2, ' ) '
COMPLEX(8) G_floating ' ( ' ,1PG23.15E3, ' , ' ,1PG23.15E3, ' ) '
COMPLEX(16) ' ( ' ,1PG42.33E4, ' , ' ,1PG42.33E4, ' ) '
CHARACTER A w 1

1Where w is the length of the character expression.

By default, character constants are not delimited by apostrophes or quotation marks, and each internal apostrophe or quotation mark is represented externally by one apostrophe or quotation mark.

This behavior can be changed by the DELIM specifier (in an OPEN statement) as follows:

  • If the file is opened with the DELIM= ' QUOTE ' specifier, character constants are delimited by quotation marks and each internal quotation mark is represented externally by two consecutive quotation marks.
  • If the file is opened with the DELIM= ' APOSTROPHE ' specifier, character constants are delimited by apostrophes and each internal apostrophe is represented externally by two consecutive apostrophes.


Previous Next Contents Index