Link to home
Start Free TrialLog in
Avatar of chenwei
chenwei

asked on

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?
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


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

Avatar of chenwei
chenwei

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chenwei

ASKER

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?
That's correct.  At least on most COBOL implementations.

Kdo

Avatar of chenwei

ASKER

Thanks and sorry for the late reply.