COBOL File Read

COBOL File Processing COBOL File Write

Advertisements


The main purpose of the READ statement is that to read the records from the file.

At a time one record can be READ from a file.


Syntax:

READ File-name
	[INTO File-rec]
	[KEY IS File-key]
	[AT END Statements]
	[INVALID KEY Statements]
	[NOT AT END Statements]
	[NOT INVALID KEY Statements]
END-READ.

INTO Clause is used to store the retrieved record into the File-rec which is provided in the READ.

INTO Clause is used for Fixed length record mostly and it is not preferred to use it for variable length records because of the length not known.

KEY Clause is used to access the records randomly by passing the key before READ.

KEY Clause used for only Indexed files concept.

AT END and NOT AT END is used for Sequential files reading to verify the END OF the FILE reached or not.

INVALID KEY and NOT INVALID KEY is used to validate the passed key is valid or not when the file is reading randomly.

INVALID KEY and NOT INVALID KEY option is available for other than indexed files.

READ syntax will differ for sequential, Indexed/Relative files.


SequentialIndexed

READ File-name
	[INTO File-rec]
	[AT END Statements]
	[NOT AT END Statements]
END-READ.
READ File-name
	[INTO File-rec]
	[KEY IS File-key]
	[INVALID KEY Statements]
	[NOT INVALID KEY Statements]
END-READ.

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.                                                    


COBOL File Processing COBOL File Write

Advertisements