2
votes

I recently got an assignment to use the "EXTEND OPEN" command to add data to an already existing file. While I thought I understood what it did I find that my program is hitting a snag, giving me this error: "148 Wrong open mode or access mode for write". It is a simple program, meant to only add one new file of entries to a master file, but I can't quite get it. Here is what I have:

   DATA DIVISION.
   FILE SECTION.

   FD  OLD-MASTER-IN.
   01  OLD-MASTER-REC-IN.
       05  O-STATE-ABREV-IN                PIC XX.
       05                                  PIC X.
       05  O-STATE-NAME-IN                 PIC X(17).

   FD  TRANS-FILE-IN.
   01  TRANS-REC-IN.
       05  N-STATE-ABREV-IN                PIC XX.
       05                                  PIC X.
       05  N-STATE-NAME-IN                 PIC X(17).

   WORKING-STORAGE SECTION.
   01  ARE-THERE-MORE-RECORDS              PIC X(3)  VALUE 'YES'.

   PROCEDURE DIVISION.
   100-MAIN.
       OPEN INPUT OLD-MASTER-IN
            EXTEND TRANS-FILE-IN

       PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'NO '
           READ OLD-MASTER-IN
               AT END
                   MOVE 'NO' TO ARE-THERE-MORE-RECORDS
               NOT AT END
                   PERFORM 200-REGULAR-UPDATE
           END-READ
       END-PERFORM

       CLOSE OLD-MASTER-IN
             TRANS-FILE-IN
       STOP RUN.

   200-REGULAR-UPDATE.
       WRITE OLD-MASTER-REC-IN FROM TRANS-REC-IN.

Any Help will be appreciated.

2
@nmichaels: Yes they are. Much of the financial system still runs Cobol on big iron. If you want to make a lot of money in a niche market doing maintenance coding, Cobol is the place to be. But of course all of you young whippersnappers want to be trendy, and coding in Cobol is like wearing a tweed suit and plaid socks, so you wouldn't touch it with a ten-foot pole, would you? - Robert Harvey
@Robert: It's not so much about wanting to be trendy as wanting to use modern language features, like lower case letters and fancy new operators like +. - nmichaels
@nmichaels -- Cobol has support low-case and format free source for decades. And fancy new operators like + - * / have been available equally long. - Joe Zitzelberger
@Joe: Okay, so that wasn't a real criticism of the language as it is today, but I wanted to keep the tone light and snarky to avoid the flames. I think this is a good time to end this thread. - nmichaels
It's a typo, see the self-answer by the OP. - Bill Woodger

2 Answers

2
votes

I figured it out. I really stupid now. Seems I extended/read the wrong file. Silly me. The code is suppose to look like this:

   PROCEDURE DIVISION.
   100-MAIN.
       OPEN INPUT TRANS-FILE-IN
            EXTEND OLD-MASTER-IN

       PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'NO '
           READ TRANS-FILE-IN
               AT END
                   MOVE 'NO' TO ARE-THERE-MORE-RECORDS
               NOT AT END
                   PERFORM 200-REGULAR-UPDATE
           END-READ
       END-PERFORM

       CLOSE TRANS-FILE-IN
             OLD-MASTER-IN
       STOP RUN.

   200-REGULAR-UPDATE.
       WRITE OLD-MASTER-REC-IN FROM TRANS-REC-IN.
0
votes

If you are writing with a modern compiler your code could be written to not look like it was written in the 70's.

Here is an example of what you could do:

OPEN INPUT TRANS-FILE-IN
OPEN EXTENT OLD-MASTER-IN

PERFORM UNTIL EXIT
    READ TRANS-FILE-IN
        AT END
        EXIT PERFORM
    END-READ
    WRITE OLD-MASTER-REC-IN FROM TRANS-REC-IN
END-PERFORM

CLOSE TRANS-FILE-IN
CLOSE OLD-MASTER-IN

Of course, there is no error checking in here but that is a large topic.