Summary -

READ statement used to read the records from the file. Only one record can be READ from the file. The file must be opened in INPUT or I-O mode to perform the READ statement. For sequential access, the READ statement makes the next logical record from a file available to the object program.

Syntax-1: sequential retrieval

Sequential Retrieval

Syntax-2: random retrieval

Random Retrieval

file-name-1 -

Specifies the file-name defined in data division FD entry.

NEXT RECORD -

Reads the next record in the logical sequence of records. NEXT is optional when the access mode is sequential and has no effect on READ statement execution. Either the NEXT phrase or the PREVIOUS phrase must specify for files reading in dynamic access mode.

PREVIOUS RECORD -

Reads the previous record in the logical sequence of records. PREVIOUS phrase applies to indexed and relative files with DYNAMIC access mode. Either the NEXT or PREVIOUS phrase must specify for files reading in dynamic access mode.

If READ...PREVIOUS specified and no previous logical record exists, the AT END condition occurs and the READ statement is unsuccessful. If READ...PREVIOUS specified, the file position indicator setting is used to determine which record to make available according to the following rules -

  • If the file position indicator indicates that no valid previous record has been established, the READ is unsuccessful.
  • If the file position indicator is positioned by the execution of an OPEN statement, the AT END condition occurs.
  • If the file position indicator is established by a previous START statement, the first existing record in the file whose relative record number (if a relative file) or whose key value (if an indexed file) is less than or equal to the file position indicator is selected.
  • If the file position indicator is established by a previous READ statement, the first existing record in the file whose relative record number (if a relative file) or whose key value (if an indexed file) is less than the file position indicator is selected.

INTO identifier-1 -

INTO Clause used to store the retrieved record into the identifier-1. identifier-1 is the receiving field. Identifier-1 must be a valid receiving the file record in accordance with the rules of the MOVE statement.

If identifier-1 is a date field, then the implied MOVE statement is performed according to the behaviour described under Moves involving date fields.

KEY IS phrase -

KEY phrase is used to access the records randomly by passing the key before READ. The KEY IS phrase can be specified only for indexed files. Data-name-1 must identify a record key associated with file-name-1. data-name-1 can be qualified however it can't be subscripted.

AT END phrase -

For sequential access, both the AT END phrase and an applicable EXCEPTION/ERROR procedure can be omitted. AT END phrase executed when no next logical record exists or an optional input file is not present.

If the AT END phrase is specified and occurred, control is transferred to imperative-statement-1 in the AT END phrase. If the AT END phrase is not specified and an applicable USE AFTER STANDARD EXCEPTION procedure exists, the procedure is executed.

Both the AT END phrase and an applicable EXCEPTION/ERROR procedure can be omitted. When the at-end condition occurs, execution of the READ statement is unsuccessful.

The contents of the associated record area are undefined. If an at-end condition does not occur during the execution of a READ statement, the AT END phrase is ignored if specified.

INVALID KEY phrase -

Both the INVALID KEY phrase and an applicable EXCEPTION/ERROR procedure can be omitted. The invalid key condition can occur during execution of a READ statement.

When an invalid key condition occurs, the READ is unsuccessful. If the INVALID KEY phrase is specified and condition occurred, control is transferred to the INVALID KEY imperative statement.

If the INVALID KEY phrase is not specified in the READ statement for a file and an applicable EXCEPTION/ERROR procedure exists, that procedure is executed. The NOT INVALID KEY phrase if specified, is ignored.

Both the INVALID KEY phrase and the EXCEPTION/ERROR procedure can be omitted. If the invalid key condition does not exist, the INVALID KEY phrase is ignored if specified.

END-READ phrase -

The END-READ used to end the scope of the READ statement. END-READ is not required when READ statement ended with period. END-READ can also be used with an imperative READ statement.

Note! If the FILE-STATUS clause is specified, the associated file status key is updated when the READ statement is executed.

Practical Example - FILE READ

Code:


  IDENTIFICATION DIVISION.                                        
  PROGRAM-ID. FILEREAD.                                           
  ENVIRONMENT DIVISION.                                           
  INPUT-OUTPUT SECTION.                                            
  FILE-CONTROL.                                                    
      SELECT FILE1 ASSIGN TO DISK1.                                
      ORGANIZATION IS SEQUENTIAL                                   
      ACCESS MODE IS SEQUENTIAL                                    
      FILE STATUS IS WS-FS.                                       
  DATA DIVISION.                                                  
  FILE SECTION.                                                   
      RECORD CONTAINS 80 CHARACTERS.                              
      BLOCK CONTAINS 800 CHARACTERS.                              
      RECORDING MODE IS F.                                        
      DATA RECORD IS STD-REC.                                      
  FD  FILE1.                                                       
  01 STD-REC.                                                     
      02 STD-NO          PIC 9(03).                               
      02 STD-NAME        PIC X(20).                                
      02 STD-GENDER      PIC X(07).                               
      02 FILLER          PIC X(50).                               
  WORKING-STORAGE SECTION.                                         
  77 WS-FS               PIC 9(02).                                
  01 WS-EOF-SW           PIC X(01) VALUE 'N'.                     
     88 EOF-SW           VALUE 'Y'.                                
     88 NOT-EOF-SW       VALUE 'N'.                                
  PROCEDURE DIVISION.                                              
      DISPLAY 'SEQUENTIAL FILE READING...'.                        
      OPEN INPUT FILE1.                                            
      PERFORM UNTIL EOF-SW                                        
         READ FILE1                                               
         AT END MOVE 'Y' TO WS-EOF-SW                              
         DISPLAY 'RECORD READ : ' STD-REC                          
      END-PERFORM.                                                 
      CLOSE FILE1.                                                 
      STOP RUN.