• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 596
  • Last Modified:

A COBOL question: open file in called-program

I have a calling-program and a called-program. In the caaling-program I've defined a file like:

Calling-Programm:
...
...
 DATA DIVISION.
****************

 FILE SECTION.
*-------------*
 FD FD-INPUT
   RECORD IS VARYING IN SIZE
     DEPENDING ON WS-EINGABE-SATZ-LAENGE.
...
...

But this file will be opened in called-program. How can I define this file in the called-program? In LINKAGE-SECTION? A short example?
0
chenwei
Asked:
chenwei
  • 3
  • 3
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

A lot of what you want to do depends on the compiler that you are using.

That being said, COBOL allows you to open a file in the main program or any subroutine.  As long as you open the file in a program (main or subprogram) and use it as a parameter to another program ("CALL" statement) you'll be fine.

Opening the file in a CALLed program is still OK, but you can not pass the address of the buffer back to the CALLing program.  But the CALLing program can pass the CALLed program a buffer in which the CALLed program copies the data it has read (with a standard MOVE statement).


...
PROGRAM-ID.  Program-1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  MY-BUFFER.
...
PROCEDURE DIVISION.
...
CALL "Program-2" USING MY-BUFFER.





...
PROGRAM-ID.  Program-1.
DATA DIVISION.
FILE SECTION.
FD  MY-FILE.
01  MY-FILE-BUFFER.
...
LINKAGE SECTION.
01  MY-BUFFER.
...
PROCEDURE DIVISION USING MY-BUFFER.

OPEN MY-FILE.
READ MY-FILE.
MOVE MY-FILE-BUFFER TO MY-BUFFER.
CLOSE MY-FILE.
...
EXIT PROGRAM.


This should be close to the functionality that you've described.


Kdo

0
 
chenweiAuthor Commented:
Hi Kdo,

As I understand what you mean is: one can't define a file in the calling-program and open it in the called-program.

The example you gave here tells: one can open the file in the called-program but the file must be defined in the called-program. Right?
0
 
Kent OlsenData Warehouse Architect / DBACommented:
That's correct.

Through some very weird magic, some COBOL compilers will let you define the same file in both a main program and one or more sub-programs with the SELECT statement.  The exact behavior may vary from one compiler to the next, but I'm certain that many compilers will behave as I'm about to describe.  The first thing to "manage" is the file's SELECT statement.

SELECT MY-FILE
ASSIGN TO "MY-TEST-FILE".

If you've got a COBOL manual for your compiler, check the SELECT statement for the optional clauses.

(A pretty good one to know is "OPTIONAL" -- if the file is opened for input and does not exist, the file is opened as if it is empty so the first READ returns end-of-file.  Some COBOLs behave that way anyway.)

The clause that should apply to your situation is "EXTERNAL" (or "EXTERN").  Here's why:

Many compilers define the name in the SELECT statement as a "global tag".  Others modify the name slightly and define the generated name.  MicroFocus, for example, takes the name, converts it to lower case, and replaces the hyphens with underscores.  "MY-FILE" becomes "my_file" and the global name "my_file" is generated.

If you have the phrase "SELECT MY-FILE" in two programs that link (load) together, you'll get a "duplicate entry point" or "duplicate reference" error.

This is where "EXTERNAL" comes in.  If the "EXTERNAL" clause is supported, it tells the compiler (and ultimately the linker) that "SELECT MY-FILE OPTIONAL" doesn't define the controls necessary to access the file, it will use the controls defined by another process.

But this has other complications.  Management of the file can be quite difficult.  It can be cumbersome to maintain the correct flags between program modules to identify if the file is open, is the mode input/output/i-o, was the last operation a read or write, etc.

It's certainly possible, but having several programs controlling a single file will generate all kinds of new and interesting programming headaches.  :)


Good Luck,
Kdo
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
chenweiAuthor Commented:
Hi Kdo,

As I understand what you mean is: one can't define a file in the calling-program and open it in the called-program.

The example you gave here tells: one can open the file in the called-program but the file must be defined in the called-program. Right?
0
 
Kent OlsenData Warehouse Architect / DBACommented:
That's correct.  At least on most COBOL implementations.

Kdo

0
 
chenweiAuthor Commented:
Thanks and sorry for the late reply.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now