Reading a file from called program



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Reading a file from called program

Postby pulsar22 » Wed Sep 11, 2013 10:48 am

Hi,

Can we read a file in called program? This file is not read in calling program. Pleas reply!
pulsar22
 
Posts: 34
Joined: Mon Sep 09, 2013 12:39 pm
Has thanked: 14 times
Been thanked: 0 time

Re: Reading a file from called program

Postby BillyBoyo » Wed Sep 11, 2013 1:05 pm

Yes, you can. You can do it in the usual way (OPEN, READ, CLOSE) all in the CALLed program, or you can have the file OPEN in the main program and the filename "passed" to the CALLed program. It depends on what you are exactly doing. If you can give more details, we may be able to make more accurate suggestions.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Reading a file from called program

Postby pulsar22 » Wed Sep 11, 2013 3:49 pm

There is a cobol stored Procedure called from various applications which are not on mainframes. I want to read a file in this SP. Since there is no JCL associated, and calling program is not a cobol code, how to read the records from this file?
pulsar22
 
Posts: 34
Joined: Mon Sep 09, 2013 12:39 pm
Has thanked: 14 times
Been thanked: 0 time

Re: Reading a file from called program

Postby dick scherrer » Wed Sep 11, 2013 7:24 pm

Hello,

Is the COBOL code invoked from these external processes?

Once invoked, the COBOL code should be able to dynamically allocate the file it needs to read.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Reading a file from called program

Postby pulsar22 » Wed Sep 11, 2013 8:56 pm

Hi,

Yes, this cobol code is invoked from external processes. So this dynamic allocation of an already existing file in cobol will be same as that of an output file?
pulsar22
 
Posts: 34
Joined: Mon Sep 09, 2013 12:39 pm
Has thanked: 14 times
Been thanked: 0 time

Re: Reading a file from called program

Postby Akatsukami » Wed Sep 11, 2013 9:03 pm

pulsar22 wrote:Hi,

Yes, this cobol code is invoked from external processes. So this dynamic allocation of an already existing file in cobol will be same as that of an output file?

Yes. You will probably want to use BPXWDYN, although other means are possible; the syntax of the command string is very similar (though not identical) to that of the TSO ALLOCATE command.
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day

These users thanked the author Akatsukami for the post:
pulsar22 (Mon Sep 16, 2013 8:52 pm)
User avatar
Akatsukami
Global moderator
 
Posts: 1058
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times

Re: Reading a file from called program

Postby dick scherrer » Wed Sep 11, 2013 9:37 pm

Hello,

You can dynamically allocate an output file as well as an input file.
Apologies for not mentioning this before . . .
Hope this helps,
d.sch.

These users thanked the author dick scherrer for the post:
pulsar22 (Mon Sep 16, 2013 8:51 pm)
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Reading a file from called program

Postby chaat » Fri Sep 20, 2013 4:54 am

in a db2 stored procedure multiple programs can execute at the same time, even multiple copies of the same program. This can lead to problems if they allocate the same file / ddname.

to get around this issue we setup a separate wlm environment with numtcb=1 and only set this one stored procedure to run in that wlm environment. It worked well for input, output files are a bit trickier as that would need to be serialized across the entire environment, not just within the wlm address space.

good luck

Chuck H.
chaat
 
Posts: 27
Joined: Sun Aug 16, 2009 11:07 pm
Location: St. Cloud, Minnesota
Has thanked: 0 time
Been thanked: 1 time

Re: Reading a file from called program

Postby pulsar22 » Mon Sep 23, 2013 8:05 pm

I have written a program for dynamically allocating and reading a file. The content of file is in one line - 'pulsar22'. It is completing with return code ' 4072'. Also, display statement for content of file shows ' " 0 " 0 " 0 " ' , something like this. Since i don't have access to applications calling this stored proc, i am executing it by JCL. Please suggest what could be the issue. The code is shown below. There are no error messages in sysout


ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INP-FILE ASSIGN TO INPFILE
FILE STATUS IS WS-STAT.
DATA DIVISION.
FILE SECTION.
FD INP-FILE.
01 WS-INP-REC PIC X(80).

WORKING-STORAGE SECTION.
01 WS-VARIABLES.
05 WS-PGM PIC X(8) VALUE 'BPXWDYN'.
05 WS-FILE-NAME PIC X(16).
05 WS-ALLOC-STRING PIC X(100).
05 WS-STAT PIC X(02).

PROCEDURE DIVISION.
MOVE 'FILE.FILE1' TO WS-FILE-NAME
STRING 'ALLOC DD(INPFILE) DSN('WS-FILE-NAME') SHR '
'CATALOG ' 'LRECL(80) RECFM(F,B)'
DELIMITED BY SIZE INTO WS-ALLOC-STRING
END-STRING.

CALL WS-PGM USING WS-ALLOC-STRING.
DISPLAY RETURN-CODE.

OPEN INPUT INP-FILE.
READ INP-FILE.

MOVE WS-INP-REC TO WS-REC
DISPLAY WS-REC
CLOSE INP-FILE.
GOBACK.
pulsar22
 
Posts: 34
Joined: Mon Sep 09, 2013 12:39 pm
Has thanked: 14 times
Been thanked: 0 time

Re: Reading a file from called program

Postby Robert Sample » Mon Sep 23, 2013 8:32 pm

I recommend you start by finding out what the dynamic allocation is doing. The return code you are testing is NOT set by BPXWDYN, so I have no idea why you are checking it after the CALL.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INP-FILE ASSIGN TO INPFILE
FILE STATUS IS WS-STAT.
DATA DIVISION.
FILE SECTION.
FD  INP-FILE.
01  WS-INP-REC PIC X(80).

WORKING-STORAGE SECTION.
01  WS-VARIABLES.
    05 WS-PGM PIC X(8) VALUE 'BPXWDYN'.
    05 WS-FILE-NAME PIC X(16).
    05 WS-ALLOC-STRING PIC X(100).
    05 WS-STAT PIC X(02).
77  WS-DYN-RC PIC S9(08) COMP-5.

PROCEDURE DIVISION.
    MOVE 'FILE.FILE1' TO WS-FILE-NAME
    STRING 'ALLOC DD(INPFILE) DSN('WS-FILE-NAME') SHR '
        'CATALOG ' 'LRECL(80) RECFM(F,B)'
        DELIMITED BY SIZE INTO WS-ALLOC-STRING
    END-STRING.

    CALL WS-PGM USING WS-ALLOC-STRING
          RETURNING WS-DYN-RC.
    DISPLAY WS-DYN-RC.
    IF  WS-DYN-RC NOT = ZERO
        STOP RUN
    END-IF.

    OPEN INPUT INP-FILE.
    READ INP-FILE.

    MOVE WS-INP-REC TO WS-REC
    DISPLAY WS-REC
    CLOSE INP-FILE.
    GOBACK.
Also, your STRING command could use some serious formatting to make better sense; if your file name is not exactly 16 bytes then you are asking for problems.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post