ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Reading a file twice in a CL program

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading a file twice in a CL program

    Hello all,

    We have a process that reads records in a file through a CL program using the RCVF command, and then processes them. I have a need to be able to read through this file a second time starting from the beginning. I don't know of a CL equivalent of the SETLL command that would reset my pointer to the beginning of the file. Any suggestions on how to do this? Would calling a second program from the first one, and reading the file the second time there, reset everything? We thought about closing the file after the first loop, but then how do we open it again within the CL? Any ideas would be appreciated. Thanks.

  • #2
    Re: Reading a file twice in a CL program

    Once you hit eof in cl no can re-read.

    there is a posdbf but again once eof no go backy backy....
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

    Comment


    • #3
      Re: Reading a file twice in a CL program

      I remember hearing something about doing a TFRCTL or some other command that makes the CL pgm behave as if it's the first time being called. However, don't recall the exact details.
      http://www.linkedin.com/in/chippermiller

      Comment


      • #4
        Re: Reading a file twice in a CL program

        Create a Wrapper CL that calls your current program to Open and Read (process) then returns to the Wrapper. Once it returns, have it call a 2nd CL that Opens and Reads the same Table and performs your 2nd job as needed.

        Comment


        • #5
          Re: Reading a file twice in a CL program

          If you are in V6R1, use the CLOSE command :

          From http://www.mcpressonline.com/program...continues.html


          Close (CLOSE) Providing Improved Database File Access
          The CLOSE command allows you to explicitly close a file that was implicitly opened with a previous RCVF operation. That may not seem like a big item at first, but the beauty of CLOSE is that it resets the database file so that the next RCVF operation to the closed database file will cause the file to be reopened. What that means is that, after all of these years, we finally have a way to reset End-Of-File within a CL program. No more need to write and call secondary CL programs in order to repetitively loop through the entire reading of a file. Now we can directly reset end of file within a CL program, and that is good news for many CL developers: less programs and less code.



          An example of using CLOSE to allow a CL program to repeatedly read all records of the file MYLIB/MYFILE is shown below. The program, after encountering each end of file message (CPF0864: "End of file detected for file &1 in &2"), closes the file and then retrieves the job's end status. If a controlled end of the job, the subsystem the job is running in, or the system itself is found, the program ends. Otherwise, the program reads MYFILE again by resuming the DoWhile loop and the RcvF operation.



          Code:
          Pgm                                                       
          DclF       File(MyLib/MyFile) OpnID(MyFile)               
          Dcl        Var(&CnlSts) Type(*Char) Len(1) Value('0')     
          
          /* Loop until a controlled cancel of job or subsystem        */ 
          DoWhile    Cond(&CnlSts = '0')                             
                     RcvF OpnID(MyFile)                             
                     MonMsg MsgID(CPF0864) Exec(Do) /* Monitor for EOF */ 
                            Close OpnID(MyFile)                     
                            RtvJobA EndSts(&CnlSts)                 
                            Iterate      /* Return to top of DoWhile   */ 
                            EndDo                                   
                     /*                                                */ 
                     /* Process record from MyFile                     */ 
                     /*                                                */ 
                     EndDo                                          
          EndPgm

          If you are in V5R4, it's necessary to write another CLP or to declare the file twice.
          Patrick

          Comment


          • #6
            Re: Reading a file twice in a CL program

            Or, define the same file twice in the CL. Read file1 to end, then read file2 for the second process step. You can define up to 5 files in CL, but CL is not really intended for reading files, and gets clunky fast.

            Comment

            Working...
            X