Search code examples
ibm-midrangerpgle

Re positioning file in CLLE


I am trying to manipulate files in CL. RCVF runs in a loop and i want to re position the file to start. I tried CLOF->OVRDBF->OPNDBF->POSDBF(*START). The subsequent RCVF is reaching end of file. Please advise on my mistake.

            PGM                                                  
            DCLF       FILE(LGLLISTP) OPNID(LGL)                 
            DCL        VAR(&FILENAME) TYPE(*CHAR) LEN(10)        

            DSPDBR     FILE(EMPMSTP) OUTPUT(*OUTFILE) +    
                         OUTFILE(QTEMP/LGLLISTP)                 

            CLOF       OPNID(LGL)                                
            OVRDBF     FILE(LGLLISTP) SHARE(*YES)                
            OPNDBF     FILE(LGLLISTP) OPTION(*INP) OPNID(LGL)    
            POSDBF     OPNID(LGL) POSITION(*START)               
READ_POS:   RCVF       OPNID(LGL)                                
            MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(READ_EXT))
            CHGVAR     VAR(&FILENAME) VALUE(&LGL_WHREFI)         
            GOTO       READ_POS                                  

READ_EXT:   RCVF       OPNID(LGL)                                    
            MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(READ_END))    
            CHGVAR     VAR(&FILENAME) VALUE(&LGL_WHREFI)             
            GOTO       READ_EXT                                      
READ_END:   CLOSE      OPNID(LGL)                                    
            ENDPGM   

Also is it possible to do key based read and reversal read (RPGLE READP equivalent) in CL programming.


Solution

  • CL has very limited file I/O functionality...

    All you can basically do is read from the start to the end. Prior to 6.1, you couldn't restart at the beginning. With 6.1 IBM added a CLOSE command allowing you to close the file and re-start the reading from the beginning.

    The following loops through a file twice. Prior to 6.1 this was not possible. The CLOSE command added at 6.1 makes it possible.

           dlcf myfile opnid(f1)
    
    loop1: 
           rcvf opnid(f1)
           momsg cpf(CPF0864) exec(goto loop1_end)
           goto loop1
    loop1_end:
          close opnid(f1)
    
    loop2:
           rcvf opnid(f1)
           momsg cpf(CPF0864) exec(goto pgm_end)
           goto loop2
    pgm_end:
           endpgm
    

    If you need READP, CHAIN, ect...then use RPGLE (or C, or Java, or ...)