cobolmainframezosjcl

Is it possible to pass a COBOL File Descriptor to another program


Can I pass a File Control FD entry in main COBOL code into a procedure?

If yes, What do I need to pass when calling the procedure from the main COBOL program?


Solution

  • Using the EXTERNAL keyword in the FD, allows the same file to be accessed by multiple programs within the run unit. Rather than "passing" the FD, the run time points the separate programs to the same FD.

    The SELECT and FD must describe the same file.

    In the following, pgm-main opens and closes the file, pgm-1 writes to the file, and pgm-2 reads the file.

       program-id. pgm-main.
       environment division.
       input-output section.
       file-control.
           select f assign "f.dat"
               organization sequential
           .
       data division.
       file section.
       fd f external.
       1 f-rec pic 99.
       procedure division.
           open output f
           call "pgm-1"
           close f
           open input f
           call "pgm-2"
           close f
           goback
           .
       end program pgm-main.
    
       program-id. pgm-1.
       environment division.
       input-output section.
       file-control.
           select f assign "f.dat"
               organization sequential
           .
       data division.
       file section.
       fd f external.
       1 f-rec pic 99.
       working-storage section.
       1 x comp pic 99.
       procedure division.
           perform varying x from 1 by 1
           until x > 5
               move x to f-rec
               write f-rec
           end-perform
           goback
           .
       end program pgm-1.
    
       program-id. pgm-2.
       environment division.
       input-output section.
       file-control.
           select f assign "f.dat"
               organization sequential
           .
       data division.
       file section.
       fd f external.
       1 f-rec pic 99.
       working-storage section.
       1 pic x value "0".
         88 eof value "1".
       procedure division.
           perform until eof
               read f
               end
                   set eof to true
               not end
                   display f-rec
               end-read
           end-perform
           goback
           .
       end program pgm-2.
    

    Output:

    01
    02
    03
    04
    05