I am always grateful for the feedback I receive from you, the readers of this web site. There are many times you mention ways of doing things that either did not think of or was unaware of. Today is a good example. I have Andrew Norton to thank for bringing to my attention a command that is a better solution for the scenario I described in the previous version of this post.

Those of us who have programmed in CL have found that when read a file when the end of file has been encountered I thought there was no way I could use the file again in the same CL program. In RPG I could use the Set Lower Limits operation, SETLL operation to reposition the file pointer. But in CL I was stuck.

01 PGM 02 DCL VAR(&LOOP) TYPE(*LGL) VALUE('1') 03 DCLF FILE(TESTFILE) OPNID(ID1) 04 OPNDBF FILE(TESTFILE) OPTION(*INP) OPNID(ID1) 05 DOWHILE COND(&LOOP) 06 RCVF OPNID(ID1) 07 MONMSG MSGID(CPF0864) EXEC(LEAVE) 08 ENDDO 09 CLOF OPNID(ID1) 10 OPNDBF FILE(TESTFILE) OPTION(*INP) OPNID(ID1) 11 RCVF OPNID(ID1) 12 CHGVAR VAR(&ID1_F1) VALUE(&ID1_F1) 13 CLOF OPNID(ID1) 14 ENDPGM

When I get to end of file within my Do loop, lines 5 – 8, even though I close the file with the CLOF command, line 9, and reopen it, line 10, I still get the CPF0864, end of file, message when I try to read the file on line 11.

If I needed read the file again I would exit this program, returning to the calling program, and call this program again. But I wanted something simpler/easier than that.

Andrew brought to my attention the CLOSE command. This command closes the file, but when the next RCVF command is encountered the file is open and the first record from the file is read. Therefore, the CL program can be reduced to just:

01 PGM 02 DCL VAR(&LOOP) TYPE(*LGL) VALUE('1') 03 DCLF FILE(TESTFILE) OPNID(ID1) 04 DOWHILE COND(&LOOP) 05 RCVF OPNID(ID1) 06 MONMSG MSGID(CPF0864) EXEC(LEAVE) 07 ENDDO 08 CLOSE OPNID(ID1) 09 RCVF OPNID(ID1) 10 CHGVAR VAR(&ID1_F1) VALUE(&ID1_F1) 11 ENDPGM

The CLOSE command, line 8, closes the file and resets the file pointer so next time I read the file, using the RCVF command on line 9, I get the first record. When I run debug and look at the value of &ID1_F1 , line 10, I can see the value from the first record. As I needed to give the file an Open Id value, line 3, the fields from the file are qualified with it.

Thank you Andrew for letting me know this very simple way to overcome the problem of resetting the file pointer to the start of the file.

If you find anything on this site that you feel you know a better way please let me know using the Contact Form on the right.

You can learn more about the CLOSE command from the IBM website here.

This article was written for IBM i 7.3, and should work for earlier releases too.