Inside COBOL #52 (Sharing open files)
by
Shawn Gordon
President
The Kompany

Have you ever run into the situation where you wanted to share files between program modules, but didn’t know how to do it? I have recently come across a couple of instances where monstrously large COBOL programs have been built instead of being broken into sub-programs, simply because the original programmers didn’t know how to share their open files. We are now in a process of breaking those programs up into manageable modules, and one of the key elements was coming up with a central file open routine for all the flat and KSAM files, where the files could be easily shared between modules.

There are only a few items that you need to be aware of, in the main program you need to specify the EXTERNAL directive in the FD statement for the file, see figure 1 for the sample source code. There is also a GLOBAL option, but this is if you intend to keep all the sub programs in one source file. This is a topic for another column, but I don’t want to promise when that will be.

The next part you are concerned with is in the subprogram making sure that you have specified ANSISUB on the $CONTROL line, and that your SELECT..ASSIGN and FD parameters match those in your main program. Take a look at Figure 2 to
see an example of the subprogram code. I have done these examples a little different than what I was talking about originally. I am opening and closing the file in the main program, and reading the file in the sub program.

As long as we are talking about dealing with files, I should probably explain why my SELECT..ASSIGN is SELECT SAMPFILE ASSIGN TO DUMMY USING SAMP-FILE. The interesting part here is “ASSIGN TO DUMMY USING SAMP-FILE”. This allows us to make the assignment of the physical file dynamic without having to resort to file equations. It means that the variable SAMP-FILE will contain the real MPE file name of the file that we are going to access. If you look closely at Figure 1, you will notice that we are prompting for the file name before we open it. This gives you a program that can open and read any file that is 80 bytes wide. A good topic for a future column is how to ready any size file without your program blowing up, which this program will if you try to read a file that is not exactly 80
bytes wide.

I think that wraps it up for this month, I appreciate the feedback I’ve been getting, as well as tips. There is some interesting stuff in the months ahead, so stay tuned.

Figure 1

$CONTROL USLINIT, BOUNDS, POST85
IDENTIFICATION DIVISION.
PROGRAM-ID. FDMAIN.
*
************************************************* 
* Test external parameter of FD statement.
* Shawn M. Gordon
************************************************* 
*
DATE-WRITTEN. MON, APR 3, 1998.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER.  HP-3000.
OBJECT-COMPUTER.  HP-3000.
SPECIAL-NAMES.
CONDITION-CODE IS CC.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SAMPFILE  ASSIGN TO DUMMY USING SAMP-FILE.

DATA DIVISION.
FILE SECTION.
FD SAMPFILE
IS EXTERNAL
RECORD CONTAINS 80 CHARACTERS.
01 SAMPFILE-REC          PIC X(80).

WORKING-STORAGE SECTION.

01 SAMP-FILE               PIC X(32)  VALUE SPACES.
*
PROCEDURE DIVISION.
A1000-OPEN.
DISPLAY 'Enter file name to read: ' NO ADVANCING. ACCEPT SAMP-FILE FREE.
IF SAMP-FILE = SPACES
   STOP RUN.
OPEN  INPUT  SAMPFILE.
CALL "FDSUB".
CLOSE  SAMPFILE.
GO TO A1000-OPEN.

Figure 2

$CONTROL USLINIT, BOUNDS, ANSISUB, POST85
IDENTIFICATION DIVISION.
PROGRAM-ID. FDSUB.
*
************************************************* 
* Test external parameter of FD statement.
* Shawn M. Gordon
************************************************* 
*
DATE-WRITTEN. MON, APR 3, 1998.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER.  HP-3000.
OBJECT-COMPUTER.  HP-3000.
SPECIAL-NAMES.
CONDITION-CODE IS CC.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SAMPFILE  ASSIGN TO DUMMY USING SAMP-FILE.

DATA DIVISION.
FILE SECTION.
FD SAMPFILE
IS EXTERNAL
RECORD CONTAINS 80 CHARACTERS.
01 SAMPFILE-REC          PIC X(80).

WORKING-STORAGE SECTION.

01 SAMP-FILE               PIC X(32).
*
PROCEDURE DIVISION.
A1000-OPEN.
READ SAMPFILE
  AT END
GOBACK.
DISPLAY SAMPFILE-REC.
GO TO A1000-OPEN.