How to reset file pointer in a CL program?

5.6k Views Asked by At

I am currently working on a CL program which needs to read some records from a file. The file is overridden to itself for one of its members. Then there are some RCVF operations to retrieve the records of that particular member. When the end of file message is received the overridden file is delete. Then another OVRDBF is performed with a different member in this file. This time when a RCVF operation is conducted it immediately hits the end of file - the message is received straight after it. Is there anyway to reset the file pointer so the subsequent RCVF operations can retrieve the content in the second member?

2

There are 2 best solutions below

5
On

You will have to manually open the file with OPNDBF and close it with CLOF as well as specify the open file identifier on the RCVF command.

            PGM

            DCLF       FILE(TESTFILE) OPNID(TESTFILE)

            /* CRTSRCPF   FILE(QTEMP/TESTFILE) */
            /* ADDPFM     FILE(QTEMP/TESTFILE) MBR(MBR1) */
            /* ADDPFM     FILE(QTEMP/TESTFILE) MBR(MBR2) */

            OPNDBF     FILE(TESTFILE) OPTION(*INP) MBR(MBR1)
MBR1:       RCVF       OPNID(TESTFILE)
            MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(MBR2))
            GOTO       CMDLBL(MBR1)

MBR2:       CLOF       OPNID(TESTFILE)
            OPNDBF     FILE(TESTFILE) OPTION(*INP) MBR(MBR2)
LOOP:       RCVF       OPNID(TESTFILE)
            MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(ENDPGM))
            GOTO       CMDLBL(LOOP)

ENDPGM:     ENDPGM
0
On

There have historically been two methods of re-reading a file in CL.

First is to use RTVMBRD to retrieve the number of records in the member. Loop through the file counting records and stop when the record count reaches the last record. This avoids getting CPF0864 thrown, so the problem is avoided.

Second is to write two programs. The first performs any needed overrides, then calls the second program to do the read loop. Upon return to the first program, the CPF0864 is cleared. A new override can be applied, and the second program can then be called again. Since it's a new invocation, it can read the same file again without problem.

In V5R3, a partial solution was provided by allowing up to five DCLFs in a single CL program. That could easily work for this case. Simply use one DCLF for the first member and a second DCLF for a different member. It can also be used to re-read the same member. (If more than five members are needed, this won't help.)

However, it still doesn't clear a CPF0864 condition for a particular DCLF.

But in i 6.1, a much more complete solution is provided. The CLOSE command was added and it will clear CPF0864. It performs a full-close, so files can easily be reused in a single CL program.