SQR While not #end-file read... not working properly

32 views Asked by At

I have to read two files in an SQR, sequentially. For both, I use While not #end-file, read xyz into $Whatever. It works fine for the first file but seems to skip on the second file. As it turns out, even when I have already done a End-While and Close 1 (first file), the variable #end-file still stays as 1. That explains when the second read comes along, it skips it.

Isn't "Close 1" supposed to reset #end-file, which up to that point, the only file that is open? I ended up having to reset #end-file to be zero manually after I do "Close 1" and that reset seems to work. Is that the right thing to do? Any input will be much appreciated. Thank you.

A reset of "Let #end-file = 0" seems to fix the problem. But I don't know if that would cause unexpected problems.

1

There are 1 answers

0
cardmagik On

If anything, one would think the next Open command would reset the flag, but it doesn't.

From what I could find, it is the READ command that sets the #End-File flag, not the open or the close.

The solution here, http://www.carlhenderson.me/2009/12/peoplesoft-sqr-file-read-problem-resolved/ suggests to change your logic to this:

WHILE 1
  READ 1 into $line:100
  IF #end-file
    BREAK
  END-IF
END-WHILE

You can verify that #End-File is set by the READ on Ray Ontko's site: https://www.ontko.com/sqr/sqrcard6.html

Search for #End-File

I think you are perfectly fine resetting the value. I don't like infinite loops, so I would write my code as in this pseudo code:

Let #End-File = 0
Open the file
read (priming read)
While not #End-file
    process the data
    read
end while

I think the important part to take away is that neither the Open nor the close control the #end-file, but the Read does.

In addition, writing code in this manner would allow for 2 files to be open at the same time, as long as you trap the #end-file accordingly. One reason to process 2 files at once is if you are doing a match-merge on 2 files. It would be a pain to keep track of the end of file for each file, but you could do it with other flags, like #end-of-file-1 and #end-of-file-2.

Hope this helps.