Reading a file twice in CL

I declared a file in CL. I read all the records, i.e End Of File has reached. Now again i want to read he file from beginning within the same program.I tried with OVRDBF and POSDBF.But could not succeed.Can any body help me?

Answer Wiki

Thanks. We'll let you know when a new response is added.


If you have V5.3 installed on your AS/400 then you declare the same file twice.

I have not yet tried on older versions.

Best of luck.

Discuss This Question: 11  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Shellvacation
    You might try the position database file command before reusing the file. POSDBF OPNID(FILENAME) POSITION(*START) Good Luck!
    0 pointsBadges:
  • Brami06
    You may want to create a second CLP that performs the reads to the file and call this from the original CLP. This way when you exit the called program, files are closed. When you call the program a second time, the file will be able to be read from the beginning without any problems. I had a similar situation recently and this worked flawlessly not to mention it broke the CLP down into managable modules (I was reading about 4 different files)
    0 pointsBadges:
  • TheQuigs
    Prior to V5R3, when you do a RCVF and it hits end of file, the system closes the file. You cannot read the same file from the same invocation of the CL again.
    0 pointsBadges:
  • Tektom
    I agree with the poster that said it's cleaner to break the process into several steps. If you must stay within the same program for some reason, you can use TFRCTL to re-invoke the program you are in, allowing you to re-open and re-process the file. To keep track of whether you're reading the file the first time or the second, you can change the job's switch settings - CHGJOB SWS('10000000') - to indicate you're in the second invocation.
    0 pointsBadges:
  • Kumari
    Thank You so much for all your messages. My AS400 machine version is V5R1. I tried with TFRCTL and it worked fine. Thank You So much guys.
    0 pointsBadges:
  • TomLiotta
    It doesn't matter what version you have. Once a CL program reaches end-of-file for any declared file, that file cannot be read again in that program. If your version supports multiple DCLFs, you can read through each one to end-of-file. This is covered in the fundamentals of CL programming. Read the manual. Tom
    125,585 pointsBadges:
  • bvining
    V6R1 has the CLOSE CL command which does allow a CL program to close a database file upon reaching end-of-file (or at any other time for that matter). The next RCVF operation to the "closed" file will cause the file to be implicitly reopened for further use
    7,070 pointsBadges:
  • Cwc
    Although this is quite an old thread, now that it has been resurrected, here's another method that I ran across a while back and saved for future reference. The key is to not let the file pointer reach the end of file condition. Thus, by knowing the number of records in the file, you can avoid doing the last read that wil cause end of file. To reposition the file pointer in CL program. 1. Use the Number of current records (NBRCURRCD) parameter of the Retrieve Member Description (RTVMBRDR) command to determine the number of active records in the member. 2. Override the database file to SHARE(*YES). This establishes an open data path (ODP) and allows the Position Database File (POSDBF) command to function properly. 3. Use the Open Database File (OPNDBF) to establish a file ID link that can be referenced by POSDBF. 4. Count the records as you process them. 5. When the record counter matches the number of active records, you have processed the last record. Use the POSDBF command to reset the file pointer to the first record. Be aware that this technique won’t work correctly if other jobs are adding records to the file, or deleting records from the file, at the same time.
    4,290 pointsBadges:
  • ejb12097
    That doesn't work. I'm on 7.1.
    20 pointsBadges:
  • Yorkshireman
    There have been significant changes to CLLE since this question was asked.
    Best to read a current manual if you are on a release after 5 (or 6!)

    6,085 pointsBadges:
  • shaychuk
    thank you Bruce it was very helpful , worked perfectly 
    75 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: