ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

dclf twice and loop inside a loop in cl

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

  • dclf twice and loop inside a loop in cl

    dear expert
    i know i can dclf 2 files now in a clle.

    but can i also loop though file a and loop file b inside file a?

    thanks

  • #2
    Re: dclf twice and loop inside a loop in cl

    loop: rcvf
    monmsg goto next
    loop1: rcvf opnid(fileB)
    monmsg goto next1
    logic
    goto cmdlbl(loop1)
    clof opnid(fileB)
    Next:
    goto cmdlbl(loop)
    next:


    but the problem now its when file B reached end of file, file A wont loop the next record.

    anyidea why and any way to solve.it?

    thanks

    Comment


    • #3
      Re: dclf twice and loop inside a loop in cl

      I'm not sure if this is a typo, but two of your lines of code should be reversed:

      clof opnid(fileB)
      Next:

      should be:

      next1:
      clof opnid(fileB)

      Comment


      • #4
        Re: dclf twice and loop inside a loop in cl

        yes it was typo, but seems it still hv the problem.
        any1 has the sample code?

        Comment


        • #5
          Re: dclf twice and loop inside a loop in cl

          ...but seems it still hv the problem.
          So far you haven't told us what the "problem" is? Is there an error message? If so, what is the message ID? Does it behave in some unexpected way? If so, what does it do wrong?

          If you have changed the code to make it different from what you posted in your question, we also need to know what it looks like now. We can't say much if we don't what the problem is and we don't know what the code looks like.
          Tom

          There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

          Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

          Comment


          • #6
            Re: dclf twice and loop inside a loop in cl

            also to add to what Tom said -- Please provide your OS version as there is no CLOSE in cl in v5r3..

            Thank you and welcome all to Friday!

            Jamie
            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


            • #7
              Re: dclf twice and loop inside a loop in cl

              I missed that. As Jamie suggests, you need to use the CLOSE OPNID(fileB) command instead of CLOF OPNID(fileB) command (assuming the problem is that it won't read records from FILEB after the first loop). If you are on a release that does not have the CLOSE command, then you'll have to split the program up into 2 programs, one to read through FILEA and one to read through FILEB.

              Comment


              • #8
                Re: dclf twice and loop inside a loop in cl

                I have read that once you hit EOF on a file there is no fixing it. I am on 7.1 and have tried both CLOSE and CLOF. Neither of them seem to work. Seems like you are going to have to have a second program that gets called like the others are suggesting to read through the second file. Here is my full code example.

                Code:
                             pgm                                                
                             dclf       file(test29a) opnid(a)                  
                             dclf       file(test29b) opnid(b)                  
                                                                                
                             opndbf file(test29a) option(*all) opnid(a)         
                                                                                
                loop:        clof opnid(b)                                      
                             monmsg msgid(CPF0000)                              
                             opndbf file(test29b) option(*all) opnid(b)         
                             rcvf opnid(a)                                      
                             monmsg     msgid(CPF0000) exec(goto cmdlbl(end))   
                                                                                
                loop1:       rcvf opnid(b)                                      
                             monmsg     msgid(CPF0000) exec(goto cmdlbl(loop))  
                             sndmsg     msg(&a_fname *tcat ' ' *tcat &b_lname) +
                                          tousr(*requester)                     
                             goto cmdlbl(loop1)                                 
                                                                                
                end:         clof opnid(a)     
                             clof opnid(b)
                             endpgm

                Comment


                • #9
                  Re: dclf twice and loop inside a loop in cl

                  Dan,
                  In your code example, you don't need the OPNDBF command, the RCVF will implicitly open the file if it is not already opened. The CLOSE command will work if the file is opened with a RCVF command. From the CLOSE command help text:
                  The Close Database File (CLOSE) command will explicitly close a
                  database file that was implicitly opened by a Receive File (RCVF)
                  command run within the same CL program or ILE CL procedure. A file
                  with the same open identifier must have been declared by a Declare
                  File (DCLF) command within the same CL program or ILE CL procedure.

                  After the CLOSE command is run, the next RCVF command for this file
                  will implicitly reopen the file and read a record from the file.

                  Comment


                  • #10
                    Re: dclf twice and loop inside a loop in cl

                    I think maybe this requires some explanation.

                    The OPNDBF, OPNQRYF, POSDBF and CLOF commands do not open, position or close the file in your program. (At least, not directly!) These commands open the file outside of your program, but inside of an operating system routine that's in your job. Why would you want to do that? Well, in new code you wouldn't... but, back in the early days, it was costly in memory and performance to have the same file opened more than once... That's not really an issue anymore today. But, back then it was much more efficient to have the file opened once, and then 'share' it (OVRBDF SHARE(*YES)) across multiple programs. It saved memory and required the OS to do less work, and back in those days (late 80's, early 90's) the systems didn't have much memory.

                    If you don't use something like OPNDBF and you do a shared open, then the open uses the open options of the first program that opens the file. Consider a batch job that calls 2 RPG programs where the first one only reads the file to print it out, and the second program updates the file with new information. If these use a shared open, you have a problem, because the shared open will open the file for reading-only because the first program to use it is a program that only reads the file. When the second program runs and tries to share the same open, you'd get an error, because the shared open is read only, and the 2nd program wants to open it for update. So, back in those days, you'd solve this problem by calling OPNDBF before your RPG programs. You'd specify "open for update" on the OPNDBF, and that would establish the open options for the shared open. When the other two programs run, there'd be no error because the file had already been opened for update.

                    CLOF closes this special opened file in the operating system.

                    So, you see, OPNDBF and CLOF do not open/close the file in your CL program. The way CL has traditionally worked is that it opens the file for you the first time you call RCVF. And it closes the file when RCVF hits end of file, or when the program ends. So you could never read the same file twice without ending the program, because once you hit end of file on RCVF, the file would close, and it could not be reopened without ending the program and restarting.

                    So, you see, this is why IBM added the CLOSE command -- it lets you close the file so that it can be re-read again.... but this was only added recently.

                    So CLOSE is a good solution, but... if anyone tells you to use CLOF or OPNDBF, they are confused.

                    Comment

                    Working...
                    X