How C program call COBOL gnt program

Description


A sample of how C program call a COBOL program (COBOL program is compiled into .gnt format)


1. C program is trying to read a record from a variable-length data set.

2. COBOL gnt is used to read out the record, and return back to C caller.


(In this sample, we only read the first record, but it can be enhanced to read any given record)


Code snippet: TESTMAIN.cpp

#include <stdio.h>

#if defined(COBOL_MICROFOCUS)
  #include <cobcall.h>
  typedef cobrtncode_t  COB_RETURN_CODE;
  typedef cobchar_t     COB_CHAR;
  typedef cobchar_t     COB_VOID;

  #define COB_INIT()

#elif defined(COBOL_COBOLIT)

  #include <libcob.h>
  typedef long          COB_RETURN_CODE;
  typedef char          COB_CHAR;
  typedef void          COB_VOID;

  #define COB_INIT()    COB_RTD = cob_get_rtd(); cob_init (rtd, 0, NULL);
#endif  

int main(int argc, char * argv[]) {

    COB_INIT();

    int length;               // the returned record length
    COB_CHAR buffer[65535];   // the returned record content
    COB_VOID * cobargv[3];
    cobargv[0] = (COB_CHAR *)&length;
    cobargv[1] = buffer;
    cobargv[2] = NULL;
    int ret = cobcall((COB_CHAR *)"COBRDREC", 2, cobargv);
    buffer[length]='\0';
    printf("Read: len[%d]=[%s]\n", length, buffer);

    return 0;
}


Code snippet: COBRDREC.cbl

       IDENTIFICATION   DIVISION.
       PROGRAM-ID.      COBRDREC.
       ENVIRONMENT      DIVISION.
       INPUT-OUTPUT     SECTION.
       FILE-CONTROL.
         SELECT INPUTFILE ASSIGN TO DATAFILE
         ORGANIZATION IS SEQUENTIAL
         FILE STATUS IS IO-STATUS.
       DATA DIVISION.
       FILE SECTION.
         FD INPUTFILE
            RECORD is VARYING in SIZE from 1 to 65535
            DEPENDING ON DATA-LEN.
         01 DATA-REC         PIC X(65535).
       WORKING-STORAGE SECTION.
         01 DATA-LEN         PIC 9(6).
         01 IO-STATUS.
            03 STATUS-KEY-1  PIC X.
            03 STATUS-KEY-2  PIC X.
            03 BINARY-STATUS REDEFINES STATUS-KEY-2 PIC 99 COMP-X.
       LINKAGE SECTION.
         01  PARM-LENGTH    PIC 9(6) COMP-5.
         01  PARM-CONTENT   PIC X(65535).

       PROCEDURE DIVISION USING PARM-LENGTH PARM-CONTENT.
*     *     DISPLAY "ENTRY OF COBRDREC".
            OPEN INPUT INPUTFILE.
            IF IO-STATUS NOT = "00"
               DISPLAY "READ OPEN FAILED IO-STATUS = [" IO-STATUS "]"
               GO TO FIN-REL
            END-IF.

            MOVE ZEROS  TO DATA-LEN.
            MOVE SPACES TO DATA-REC.
            READ INPUTFILE NEXT
              AT END GO TO NO-DATA
            END-READ.
            IF IO-STATUS NOT = "00"
               DISPLAY "READ INPUT FAILED"
               IF STATUS-KEY-1 = '9'
                  DISPLAY "FILE ERROR, STATUS: 9/" BINARY-STATUS
               ELSE
                  DISPLAY "FILE ERROR, STATUS: " IO-STATUS
               END-IF
               GO TO FIN-REL
            END-IF.

*     *     DISPLAY "L=[" DATA-LEN "],D=[" DATA-REC(1:DATA-LEN) "]".
            CLOSE INPUTFILE.
            MOVE DATA-LEN TO PARM-LENGTH.
            MOVE DATA-REC(1:DATA-LEN) TO PARM-CONTENT.
            MOVE 0 TO RETURN-CODE.
*     *     DISPLAY "EXIT OF COBRDREC".
            GOBACK.
       NO-DATA.
            DISPLAY "NO DATA FOUND".
       FIN-REL.
            CLOSE INPUTFILE.
            MOVE 1 TO RETURN-CODE.
            DISPLAY "EXIT OF COBRDREC WITH FAILURE".
            GOBACK.


The end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值