1

I'm trying to open a file as indexed but keep getting the following error. From all the examples of COBOL code that I could find I can't see where my error is.

I am able to open the file as sequential just fine. It seems to be something with trying to open it as indexed.

Error:

project2.cbl:119: libcob: Permanent file error (STATUS = 30) File : 'customers.dat'

System:

OS X

GnuCOBOL

OpenCobolIDE
Version: 4.7.3

Code:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. PROJECT-2.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.

       SELECT CUST-FILE ASSIGN TO "customers.dat"
           ORGANIZATION IS INDEXED
           ACCESS IS RANDOM
           RECORD KEY IS CUST-ID.

       SELECT INV-FILE ASSIGN TO "inventory.dat"
           ORGANIZATION IS INDEXED
           ACCESS IS RANDOM
           RECORD KEY IS ITEM-ID.

       SELECT TRANS-FILE ASSIGN TO "transactions.dat"
           ORGANIZATION IS LINE SEQUENTIAL.

       SELECT SORTED-TRANS-FILE ASSIGN TO "sorted-transactions.dat"
           ORGANIZATION IS LINE SEQUENTIAL.

       SELECT WORK-TRANS-FILE ASSIGN TO "work-transaction.dat".

   DATA DIVISION.
   FILE SECTION.

   FD  CUST-FILE.
   01  CUST-RECORD.
       05  CUST-ID         PIC 9(10).
       05  NAME            PIC A(23).
       05  STREET          PIC X(23).
       05  CITY            PIC A(13).
       05  STATE           PIC A(12).
       05  AMT-DUE         PIC 9(3).99.

   FD  INV-FILE.
   01  INV-RECORD.
       05  ITEM-ID         PIC 9(11).
       05  DESC            PIC A(24).
       05  OH              PIC 9(7).
       05  MIN-STK         PIC 9(7).
       05  PRICE           PIC 9(2).99.

   FD  TRANS-FILE.
   01  TRANS-RECORD.
       05  CUST            PIC 9(10).
       05  ITEM            PIC 9(12).
       05  QTY             PIC 9(6).
       05  SALE-CD         PIC X(1).
           88  10-OFF      VALUE "A".
           88  20-OFF      VALUE "B".
           88  25-OFF      VALUE "C".
           88  3-GET-1     VALUE "D".
           88  1-GET-1     VALUE "E".
           88  NO-DISC     VALUE "Z".

   FD  SORTED-TRANS-FILE.
   01  SORTED-TRANS-RECORD.
       05  S-CUST          PIC 9(10).
       05  S-ITEM          PIC 9(12).
       05  S-QTY           PIC 9(6).
       05  S-SALE-CD       PIC X(1).
           88  10-OFF      VALUE "A".
           88  20-OFF      VALUE "B".
           88  25-OFF      VALUE "C".
           88  3-GET-1     VALUE "D".
           88  1-GET-1     VALUE "E".
           88  NO-DISC     VALUE "Z".

   SD  WORK-TRANS-FILE.
   01  WORK-TRANS-RECORD.
       05  W-CUST          PIC 9(10).
       05  W-ITEM          PIC 9(12).
       05  W-QTY           PIC 9(6).
       05  W-SALE-CD       PIC X(1).
           88  10-OFF      VALUE "A".
           88  20-OFF      VALUE "B".
           88  25-OFF      VALUE "C".
           88  3-GET-1     VALUE "D".
           88  1-GET-1     VALUE "E".
           88  NO-DISC     VALUE "Z".

   WORKING-STORAGE SECTION.
   01  SWITCHES.
       05 C-EOF-SWITCH     PIC X VALUE "N".
       05 I-EOF-SWITCH     PIC X VALUE "N".
       05 T-EOF-SWITCH     PIC X VALUE "N".
       05 S-EOF-SWITCH     PIC X VALUE "N".

   01  COUNTERS.
       05 REC-COUNTER-C    PIC 9(3) VALUE 0.
       05 REC-COUNTER-I    PIC 9(3) VALUE 0.
       05 REC-COUNTER-T    PIC 9(3) VALUE 0.
       05 REC-COUNTER-S    PIC 9(3) VALUE 0.

   PROCEDURE DIVISION.
   MAIN-PROCEDURE.

       PERFORM 100-INITIALIZE.
       PERFORM 200-SORT-TRANSACTIONS.
       PERFORM 300-CUSTOMER-INVOICE
           UNTIL S-EOF-SWITCH = "Y".
       PERFORM 900-TERMINATE.

       STOP RUN.

   100-INITIALIZE.
       OPEN I-O CUST-FILE.
       READ CUST-FILE
           AT END
               MOVE "Y" TO C-EOF-SWITCH
           NOT AT END
               COMPUTE REC-COUNTER-C = REC-COUNTER-C + 1
       END-READ.

       OPEN I-O INV-FILE.
       READ INV-FILE
           AT END
               MOVE "Y" TO I-EOF-SWITCH
           NOT AT END
               COMPUTE REC-COUNTER-I = REC-COUNTER-I + 1
       END-READ.

       OPEN INPUT TRANS-FILE.
       READ TRANS-FILE
           AT END
               MOVE "Y" TO T-EOF-SWITCH
           NOT AT END
               COMPUTE REC-COUNTER-T = REC-COUNTER-T + 1
       END-READ.

   200-SORT-TRANSACTIONS.
       SORT WORK-TRANS-FILE ON ASCENDING W-CUST
       USING  TRANS-FILE
       GIVING SORTED-TRANS-FILE.

   300-CUSTOMER-INVOICE.

       DISPLAY S-CUST.
       DISPLAY S-ITEM.
       DISPLAY S-QTY.
       DISPLAY S-SALE-CD.
       DISPLAY " ".

       READ SORTED-TRANS-FILE
           AT END
               MOVE "Y" TO S-EOF-SWITCH
           NOT AT END
               COMPUTE REC-COUNTER-S = REC-COUNTER-S + 1
       END-READ.

   900-TERMINATE.
       DISPLAY "Number of Customers: " REC-COUNTER-C.
       DISPLAY "Number of Transactions: " REC-COUNTER-T.
       DISPLAY "Number of Inventory Items: " REC-COUNTER-I.
       CLOSE CUST-FILE.
       CLOSE INV-FILE.
       CLOSE TRANS-FILE.

   END PROGRAM PROJECT-2.

File Content:

01001     Steve Howe             123 Topographic Rd     London       England     000.00
01002     Geddy Lee              4385 Xanadu Ln         Toronto      Canada      058.15
01003     Steve Hackett          16 Serpentine Dr       London       England     134.10
01005     Nancy Wilson           5763 Butterfly St      Seattle      Washington  018.95
01008     Andy Latimer           858 Sasquatch St       Leeds        England     857.44
01015     Dweezil Zappa          86 Yerbouti Blvd       Los Angeles  California  000.00
01019     Roine Stolt            2332 Retropolis        Stockholm    Sweden      069.95
01023     Tal Wilkenfeld         52525 Beck Way         Sydney       Australia   200.00
01044     Todd Rundgren          662 Utopia St          Los Angeles  California  157.21
01088     Mike Rutherford        91 N Broadway          New York     New York    000.00
Iron3eagle
  • 1,077
  • 7
  • 23
  • 1
    Simon Sobisch is correct. There is no magic whereby you can present a text file from an editor (or any other source) to a COBOL program and use it as an indexed file just by saying INDEXED on the SELECT. Assuming that you have indexed support for your installation of GnuCOBOL, creating an indexed file in one COBOL program and using it in another is a trivial exercise. Open two files (one input, one output) read the text, write to indexed, continue until end, close both the files. Now you have an indexed file. – Bill Woodger Jul 13 '16 at 18:59
  • @BillWoodger Thanks for explaining the lack of magic and how trivial and simple it is to convert the file. That was very constructive. – Iron3eagle Jul 13 '16 at 19:02
  • Yeah, well, I forgot the really important bit of "the text file must be already in key sequence" :-) – Bill Woodger Jul 13 '16 at 20:50

1 Answers1

6

A sequential file cannot be opened as an indexed file. It is a completely different file type.

You may want to OPEN OUTPUT CUST-FILE, read your customers from the sequential file and WRITE CUST-FILE FROM seq-record. Afterwards your OPEN INPUT CUST-FILE will work (depending on what you want to achieve it may be better to read the data into a temporary and ordered table, then use SEARCH ALL to get a specific record.

Simon Sobisch
  • 6,263
  • 1
  • 18
  • 38
  • Forgive my ignorance on this matter, but what defines a file as sequential and not indexed? The file itself is essential a text file created with a standard editor. I'll post the contents in question. – Iron3eagle Jul 13 '16 at 18:04
  • 2
    For a Text file you would use the Line-Sequential option when defining the file in Cobol. A indexed file consists of Data-File + one (or possibly more) index files. The index files consists of a Keys + pointers to records in the data-file. See B-Tree: https://en.wikipedia.org/wiki/B-tree – Bruce Martin Jul 13 '16 at 18:29
  • @BruceMartin Thanks for the explanation. I'm now in the process of converting the file. I appreciate the constructive comment. – Iron3eagle Jul 13 '16 at 19:03