RPG/400 Help

READ (Read a Record)

*---------*---------------*-----------------*----------------*---------------*
|   CODE  |    FACTOR 1   |     FACTOR 2    |     RESULT     |  INDICATORS   |
|         |               |                 |      FIELD     |               |
*---------*---------------*-----------------*----------------*---------------*
| READ    |               | File name,      | Data           |   _ ER EOF    |
| (N)     |               | Record name     | structure      |               |
*---------*---------------*-----------------*----------------*---------------*

The READ operation reads the record, currently pointed to, from a full procedural file (identified by an F in position 16 of the file description specifications).

Factor 2 must contain the name of a file. A record format name in factor 2 is allowed only with an externally described file (E in position 19 of the file description specifications). It may be the case that a READ-by-format-name operation will receive a different format than the one you specified in factor 2. If so, your READ operation ends in error.

The result field can contain the name of a data structure into which the record is read only if the file named in factor 2 is a program described file (identified by an F in position 19 of the file description specifications). See File Operations for information on how data is transferred between the file and the data structure.

If a READ operation is successful, the file is positioned at the next record that satisfies the read. If either indicator is set on, you must reposition the file (by a CHAIN (Random Retrieval from a File), SETLL (Set Lower Limit), or SETGT (Set Greater Than) operation).

If the file from which you are reading is an update disk file, you can specify an N in position 53 to indicate that no lock should be placed on the record when it is read. See the RPG/400* User's Guide for more information.

You can specify an indicator in positions 56 and 57 to be set on if the READ operation is not completed successfully. If an error occurs when no indicator is specified, control passes to your INFSR subroutine (if specified) or the default error/exception handler.

You must specify an indicator in positions 58 and 59 to signal whether end of file occurred on the READ operation. The file must be repositioned after the indicator is set on to process any further successful sequential operation (for example, READ or READP) to the file. This indicator is set on or off every time the READ operation is performed.

Figure "READ Operation" illustrates the READ operation.

When you specify a multiple device file in factor 2, the READ operation does one of:

When you specify a format name in factor 2, and the format name is associated with a multiple device file, data is read from the device identified by the field specified in the ID entry on the file continuation specifications. If there is no such entry, data is read from the device used in the last successful input operation.

READ Example

Figure 45. READ Operation

*...1....+....2....+....3....+....4....+....5....+....6....+....7...
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++
C*
C*  READ retrieves the next record from the file FILEA, which must
C*  be a full procedural file.  Indicator 61 is set on if end of
C*  file occurs on READ, or if end of file has occurred previously
C*  and the file has not been repositioned.  When indicator 61
C*  is set on, the program branches to the label (EOF) specified
C*  in the GOTO statement.
C                     READ FILEA                    61 END OF FILE
C   61                GOTO EOF
C*
C*  READ retrieves the next record of the type REC1 (factor 2)
C*  from an externally described file.  (REC1 is a record format
C*  name.)  Indicator 64 is set on if end of file occurs on READ, or
C*  or if it has occurred previously and the file has not been
C*  repositioned.  When indicator 64 is set on, the program
C*  branches to the label (EOF) specified in the GOTO statement.
C*  N in position 53 indicates that the record is not locked.
C*
C                     READ REC1                N    64 END OF FILE
C   64                GOTO EOF
C*
C           EOF       TAG


[ Top of Page | Previous Page | Next Page | Table of Contents ]