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?
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?
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
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 neededFile-Drivers are almost identical. You can