Previous Topic Next topic Print topic


Code Sample

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.

  1. Enter the following to the newly created dbacct.cbl program:
    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.
  2. After you have entered the program above, save the file and then build the project.

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:

  1. Close your solution and exit Studio Enterprise Edition.
  2. Open a Visual Studio command prompt and set a variable:
    Variable Value
    DD_ACCTFIL     <location of file>\ACCTFIL
  3. Start Studio Enterprise Edition from the command prompt using the devenv command.

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:

  1. Make a copy of acctrec.cpy called acctrec2.cpy and modify it as follows:
    02  ACCTDO               PIC X(5).
               02  SNAMEDO.
                   05  SNAMEDO1         PIC X(12).
                   05  SNAMEDO2         PIC X(6).
               02  FNAMEDO              PIC X(12).
    
    
  2. Once you have modified the new file and saved it, add it to your project.
  3. Change the copy statement in the COBOL application to copy in the new file acctrec2.cpy.
  4. Change the select statement to read:
    ALTERNATE RECORD KEY IS
    SNAMEDO1 OF ACCT-IN-REC WITH DUPLICATES
  5. Change the FD statement to read:
    FD ACCT-IN
        RECORD IS VARYING FROM 63 TO 383.
    01 ACCT-IN-REC.
    COPY “acctrec2.cpy”.
    
  6. Rebuild your project and run the application.

You should now have a COBOL application that successfully opens the file for processing.

Previous Topic Next topic Print topic