T_T
T_T

Reputation: 13

Reading the last record of a file

I have a COBOL program that requires a transaction number every run of the program. I am planning to get the last transaction number from the file and add 1 to it. The problem is I don't know how to get the last recorded value.

IDENTIFICATION DIVISION.
PROGRAM-ID. INVENTORY-SYS.
AUTHOR. LINSEY.
DATE-WRITTEN. 2/22/2015.
DATE-COMPILED. 2/22/2015.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
    SELECT MASTER-FILE ASSIGN TO "inventory-file.txt"
    ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD MASTER-FILE.
01 IN-RECORDS.
    02 IN-CODE PIC 9(7).
    02 IN-NAME PIC X(30).
    02 IN-PRICE PIC 9(3).
    02 IN-STOCK PIC 9(4).

WORKING-STORAGE SECTION.
01 WS-EOF PIC A(1).

PROCEDURE DIVISION.
100-READ-FILE.
OPEN I-O MASTER-FILE.
PERFORM UNTIL WS-EOF = "Y"
READ MASTER-FILE
 AT END
      MOVE 'Y' TO WS-EOF
 NOT AT END
    DISPLAY IN-RECORDS
END-READ
END-PERFORM
CLOSE MASTER-FILE.
STOP RUN.

This is the sample program. The problem is it retrieves all the records from the file I only the last record from "inventory-file.txt"

Upvotes: 0

Views: 6204

Answers (2)

Joe Zitzelberger
Joe Zitzelberger

Reputation: 4263

I'm not sure of OpenCobol supports it, but you might try using OPEN/REVERSED. It is an old school way of reading a tape backwards, or these days, a virtual tape. I've no idea if it is implemented, but it is an easy way to read a sequential file backwards.

Upvotes: 0

Bill Woodger
Bill Woodger

Reputation: 13076

Well, you are insistent, so:

IDENTIFICATION DIVISION.
PROGRAM-ID. INVENTORY-SYS.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
    SELECT MASTER-FILE ASSIGN TO "inventory-file.txt"
    ORGANIZATION IS LINE SEQUENTIAL
    FILE STATUS IS w-if-in-file-status.

DATA DIVISION.
FILE SECTION.
FD MASTER-FILE.
01 IN-RECORDS.
    02 IN-CODE PIC 9(7).
    02 IN-NAME PIC X(30).
    02 IN-PRICE PIC 9(3).
    02 IN-STOCK PIC 9(4).

WORKING-STORAGE SECTION.
01  w-if-in-file-status PIC XX.
    88  master-file-status-good VALUE ZERO "10".
    88  end-of-master-file VALUE "10".
01  w-save-code PIC 9(7).

PROCEDURE DIVISION.
    OPEN INPUT MASTER-FILE
    [code to check FILE STATUS field]
    PERFORM UNTIL end-of-master-file
        READ MASTER-FILE
        [code to check FILE STATUS field]
        MOVE IN-CODE to w-save-code
    END-PERFORM
    DISPLAY ">" w-save-code "<"
    CLOSE MASTER-FILE
    [code to check FILE STATUS field]
    GOBACK
    .

I don't know why your IN-CODE field is numeric. Are you going to do calculations with it?


You keep a Control File. That has a date (to match to the Business Date file) a logical file-name, an environment and the last transaction number.

You maintain that file, checking everything as you do so.

Upvotes: 0

Related Questions