To begin the process, you write a small application to open and close the file. This allows you to check to see if our COBOL description of the file is correct.
From the previous section, you know that the record layout of the file is located in the copy book ACCTREC. You also know that you have two keys on the file. There is a primary key that is located at offset 1 and has a length of 5 bytes. There is also an alternate key located at offset 6 that is 12 bytes in length and allows duplicates. You fill in the initial COBOL structure using this information.
To get the key information, scan down the record description and locate COBOL fields at the correct offset.
IDENTIFICATION DIVISION. PROGRAM-ID. DBACCT. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT ACCT-IN ASSIGN TO DISK "ACCTFIL" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS ACCTDO OF ACCT-IN-REC ALTERNATE RECORD KEY IS SNAMEDO OF ACCT-IN-REC WITH DUPLICATES FILE STATUS IS ACCT-FILE-STATUS. DATA DIVISION. FILE SECTION. FD ACCT-IN. 01 ACCT-IN-REC. COPY "acctrec.cpy". WORKING-STORAGE SECTION. 01 ACCT-FILE-STATUS. 05 ACCT-STATUS-KEY-1 PIC X(1). 05 ACCT-STATUS-KEY-2 PIC X(1). 05 ACCT-STATUS-KEY-2-BINARY REDEFINES ACCT-STATUS-KEY-2 PIC 9(2) COMP-X. 01 USER-RETURN PIC X(10). PROCEDURE DIVISION. DECLARATIVES. FILE-ERR-HANDLING SECTION. USE AFTER STANDARD ERROR PROCEDURE ON ACCT-IN. FILE-ERR. DISPLAY "File Error: ", ACCT-FILE-STATUS. DISPLAY "Press <return> to exit". ACCEPT USER-RETURN. STOP RUN. END DECLARATIVES. LEVEL-1 SECTION. MAIN-LOGIC. PERFORM OPEN-IN-FILE. STOP RUN. OPEN-IN-FILE. OPEN INPUT ACCT-IN. CLOSE ACCT-IN.
You can now run the project in the Debugger. However, when your application attempts to open the file, the following error message appears:
File Error: 35 Press <return> to exit
You have not told the program where the file is located. To do this in Studio Enterprise Edition, follow the steps below:
Variable | Value |
---|---|
DD_ACCTFIL | <location of file>\ACCTFIL |
This time when you run the file, the following error message appears.
File Error: 39 Press <return> to exit
This error indicates that there is a mismatch between the file description in our COBOL program and the actual file on disk. Reviewing the information from the previous section, you see that the first alternate key for the file should be only 12 bytes long. The field you selected for the alternate record key from the copy book, however, is 18 bytes long. You need to break this field up:
02 ACCTDO PIC X(5). 02 SNAMEDO. 05 SNAMEDO1 PIC X(12). 05 SNAMEDO2 PIC X(6). 02 FNAMEDO PIC X(12).
ALTERNATE RECORD KEY IS SNAMEDO1 OF ACCT-IN-REC WITH DUPLICATES
FD ACCT-IN RECORD IS VARYING FROM 63 TO 383. 01 ACCT-IN-REC. COPY “acctrec2.cpy”.
You should now have a COBOL application that successfully opens the file for processing.