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

Viewed 281

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?

2 Answers

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

This is an alternative approach rather than a direct answer to the question. Its an approach Cobol programmers may across and should know about. I added for information purposes.


An alternative approach to passing a File-Control around is to use a File-Driver or File-Interface module that does the Reading/Writing of the file. The individual applications programs call this program, this approach works best if a file is used in multiple places.

There are advantages of a File-Driver include

  • You can make the file-record-area in the Linkage much larger than file-record size. The file might have a 50 byte record with 20 byte filler, you could make the File-Record-Linkage area 500 bytes. That may reduce maintenance in the future.
  • Typically Control records / header / footer records can be written / checked in the File-Driver program. The application programs do not need to know about any control records. At one site I needed to add Header/Footer and file validation to an existing file. All I needed to was change Code Generation parameters, regenerate the File-Driver and some basic testing. No program change was needed
  • File-Drivers are almost identical. You can
    • Store the bulk of the Working Storage and Code in Copybooks and use Copy replacing.
    • Use Code generation
    • Standard Copy modify
  • File drivers can be tested separately from the calling program. That is handy when you are using file validation.
Related