COBOL File Read Next

COBOL File Start COBOL File Close

Advertisements


READ NEXT is used to the records sequentially after the START statement placed the cursor based on the key provided.

READ NEXT also used in DYNAMIC mode only where the START command used.


Syntax:

READ NEXT filename
	[INTO file-rec]
	[INVALID KEY statement block]
END-READ.

READ NEXT will only used to read the records.


Practical Example - FILE READ NEXT:


Below example is used to display all the records after the specified student number from student records.


Code:


 IDENTIFICATION DIVISION.                                         
 PROGRAM-ID. INDXFILD.                                            
 ENVIRONMENT DIVISION.                                            
 INPUT-OUTPUT SECTION.                                            
 FILE-CONTROL.                                                    
     SELECT FILE1 ASSIGN TO DISK1.                               
     ORGANIZATION IS INDEXED.                                    
     ACCESS MODE IS DYNAMIC.                                     
     RECORD KEY  IS STD-NO.                                       
     FILE STATUS IS WS-FS.                                        
 DATA DIVISION.                                                   
 FILE SECTION.                                                    
 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.                                            
     ACCEPT STD-NO.                                             
     DISPLAY 'FILE BROWSING...'.                                 
     OPEN INPUT FILE1.                                           

     START FILE1 KEY EQUAL STD-NO.
     INVALID KEY DISPLAY 'NO STUDENT FOUND WITH GIVEN STD-NO'
     NOT INVALID KEY PERFORM READ-NEXT-RECS. 

     CLOSE FILE1.                                                
     DISPLAY STD-REC.  

     STOP RUN.                                                    

 READ-NEXT-RECS.

     PERFORM UNTIL EOF-SW                                         
        READ NEXT FILE1                                           
        AT END MOVE 'Y'  TO WS-EOF-SW                             
           DISPLAY 'RECORD READ : ' STD-REC                       
     END-PERFORM. 

 READ-NEXT-RECS-EXIT.
     EXIT.


COBOL File Start COBOL File Close

Advertisements