Monday, September 26, 2011

COBOL : Sample program for Dynamic file allocation



IBM supplied routine BPXWDYN can be used to dynamically allocate files in batch programs. The below program dynamically creates a new dataset and allocates it to DDname OTFILE and then it opens the file in OUTPUT mode and writes a record to it.

IDENTIFICATION DIVISION.
PROGRAM-ID. test.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT OT-FILE ASSIGN TO OTFILE
FILE STATUS FS.
DATA DIVISION.
FILE SECTION.
FD OT-FILE.
01 OT-REC PIC X(80).
WORKING-STORAGE SECTION.
01 FS PIC X(02).
01 FN PIC X(16).
01 WS-ALLOC-STRING PIC X(100).
01 PGM PIC X(08) VALUE 'BPXWDYN'.
PROCEDURE DIVISION.
    MOVE 'TSU5FED.TSTS.DYN' TO FN.
    STRING 'ALLOC DD(OTFILE) DSN(' FN ') NEW '
           'CATALOG ' 'LRECL(80) RECFM(F,B)'
    DELIMITED BY SIZE
     INTO WS-ALLOC-STRING
    END-STRING.
    DISPLAY ' WS-ALLOC-STRING *' WS-ALLOC-STRING '*'
    CALL PGM USING WS-ALLOC-STRING.
    DISPLAY RETURN-CODE.
    OPEN OUTPUT OT-FILE.
    MOVE 'MY REC' TO OT-REC.
    WRITE OT-REC.
    DISPLAY 'FS :' FS
    CLOSE OT-FILE.
    GOBACK.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.