?
Solved

A COBOL question: open file in called-program

Posted on 2003-03-05
6
Medium Priority
?
557 Views
Last Modified: 2013-11-25
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
Comment
Question by:chenwei
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8076392

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
 

Author Comment

by:chenwei
ID: 8078809
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
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 80 total points
ID: 8079662
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:chenwei
ID: 8086435
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8088700
That's correct.  At least on most COBOL implementations.

Kdo

0
 

Author Comment

by:chenwei
ID: 8101141
Thanks and sorry for the late reply.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question