读取文件的最后一条记录

时间:2015-03-03 07:59:04

标签: cobol

我有一个COBOL程序,每次运行程序都需要一个事务编号。我打算从文件中获取最后一个交易号,并为其添加1。问题是我不知道如何获得最后记录的值。

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.

这是示例程序。问题是它从文件中检索了所有记录我只记录了" inventory-file.txt"中的最后一条记录。

2 个答案:

答案 0 :(得分:0)

嗯,你是坚持的,所以:

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
    .

我不知道为什么你的IN-CODE字段是数字的。你打算用它做计算吗?


您保留控制文件。它有一个日期(与业务日期文件匹配)一个逻辑文件名,一个环境和最后一个交易号。

您维护该文件,并在执行此操作时检查所有内容。

答案 1 :(得分:0)

我不确定OpenCobol是否支持它,但您可以尝试使用OPEN / REVERSED。这是一种旧式的学习方式,可以向后读取磁带,或者现在是一种虚拟磁带。我不知道它是否已实现,但它是向后读取顺序文件的简单方法。