Click here for RAC Consulting sponsor message |
Sharing Open Files
By Shawn M.
Gordon
Have you ever run into a situation where you wanted to
share files between program modules, but didnt know how? 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 didnt know how to share their open
files.
We are now in the process of breaking those programs up into manageable modules. 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 to do this. In the main program you need to specify the EXTERNAL directive in the FD statement for the file; see Figure 1 below for the sample source code.
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.
There is also a GLOBAL option, but this is if you intend to keep all the sub programs in one source file. That is a topic for another column, but I dont want to promise when Ill write it.
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 below to see an example of the subprogram code.
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.
I have done these examples a little differently 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 read 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.
That wraps it up for this month. I appreciate the feedback Ive been getting, as well as the tips. Keep those cards and letters coming, and earn yourself a free 3000 Always Online hat for tips you submit, either to the NewsWire or to me at smga@compuserve.com.
Shawn Gordon, whose S.M. Gordon & Associates firm
supplies HP 3000 utilities, has worked with 3000s since 1983.Shawn M.
Gordon, whose S.M. Gordon & Associates firm supplies HP 3000 utilities,
has worked with 3000s since 1983.
Copyright 1998, The 3000 NewsWire. All rights
reserved.