Wednesday, June 7, 2017

Repositioning file pointer after EOF in CL

stop end of file from happening in a cl program

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.

7 comments:

  1. Can't you just use the POSDBF command like so -

    POSDBF OPNID(FILENAME) POSITION(*START)

    ReplyDelete
    Replies
    1. Nope. Once end of file has been encountered you cannot reset the file pointer using POSDBF. I tried using it in the testing for this post.

      Delete
  2. ILDEFONSO SERAFINJune 11, 2017 at 12:41 PM

    thanks for this advice.

    ReplyDelete
  3. What actually difference between CLOF AND CLOSE command?

    ReplyDelete
    Replies
    1. In this case the main the difference I have found between the CLOF and CLOSE commands is that the CLOF does not allow you to reset the file pointer.
      Otherwise I have found them the same.

      Delete
  4. As far as I remember, if the file is multi-membered, the RCVF after CLOSE will fetch the first record from next member in the file. So, we need to be careful about this when reading multi-membered files in CL and are using CLOSE command.

    ReplyDelete
    Replies
    1. You can insert the OPNDBF command before opening to specify the member to use.

      Delete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.