读取顺序输入文件的模式 [英] Pattern for reading a sequential input file

查看:46
本文介绍了读取顺序输入文件的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直看到很多示例,这些示例用于读取COBOL中的顺序文件,如下所示:

I've been seeing a lot of examples for reading a sequential file in COBOL that looks something like this:

FD File-Record
01  Input-Record.
    88  End-Of-File       VALUE HIGH-VALUES.
    05  ...

...

    READ File-Record
        AT END SET End-Of-File TO TRUE
    END-READ

    PERFORM UNTIL End-Of-File
        PERFORM Process-Record
        READ File-Record
            AT END SET End-Of-File TO TRUE
        END-READ
    END-PERFORM

一个问题是,是否也一样对其进行如下处理?

One question is, would it be just as well to process it as follows?

    PERFORM UNTIL End-Of-File
        READ File-Record
            AT END SET End-Of-File TO TRUE
            NOT AT END PERFORM Process-Record
        END-READ
    END-PERFORM

我通常没有看到第二种模式,但是它对我来说似乎更简洁和多余。与第一个有问题吗?我指的不是上述主题的变体(根据您定义 PERFORM 的方式,可以对它们进行不同的分组),但我指的是第一记录预读模式,在我所看到的各种示例中似乎都喜欢这种模式。

I've not generally seen the second pattern, but it seems more concise and less redundant to me. Is there a problem with it versus the first? I'm not referring to variations on the theme above (they can be grouped differently depending upon how you define what you PERFORM on) but I'm referring to the concept of the first record pre-read pattern, which seems to be favored in various examples I've seen.

推荐答案

第一个称为预读。使用此方法意味着总有一条记录可以进入处理循环。

The first is called a "Priming Read". Using this means there is always a record available going into the processing loop.

第二个被称为...嗯,不确定它的名字。在处理循环内,必须测试记录的可用性。

The second is called... well, not sure that's got a name. Within the processing loop the availability of a record has to be tested for.

有几件事。使用 AT END / NOT END / END-READ 本身有点笨拙(意见)。有一种更清洁的方法(有两个原因使其更清洁)。

A couple of things. Using the AT END/NOT AT END/END-READ is in itself somewhat unwieldy (opinion). There is a cleaner way (two reasons for it being cleaner).

在文件的SELECT语句(您应该对所有文件都执行此操作)上定义文件状态字段,每个文件一个。

On the SELECT statement for your file (you should do this for all files) define a FILE STATUS field, individual per file.

每次访问文件后,测试该文件的文件状态字段,然后确保访问获得了预期的结果。

After every file-access, test the file-status field for that file, and ensure the access gave an expected result.

使用此方法,文件状态字段将自动设置为 10 到达文件末尾。因此,将88移到文件状态字段,然后将 VALUE 更改为10。

Using this method, the file-status field will be automatically set to 10 when end-of-file is reached. So you move the 88 to the file-status field, and change the VALUE to 10.

01  INPUT-FILE-STATUS                PIC XX.
    88  INPUT-FILE-OK                VALUE ZERO "10".
    88  INPUT-FILE-EOF               VALUE "10".

PERFORM                       PRIMING-READ
PERFORM UNTIL End-Of-File
    PERFORM                   Process-Record
    PERFORM                   READ-A-RECORD05 is optional file not present, 23 is record not found.
END-PERFORM

...
PRIMING-READ.
    PERFORM                   READ-A-RECORD
    IF INPUT-FILE-EOF
        [cancel with end-of-file on first read message]
    END-IF
    .

READ-A-RECORD.
    READ File-Record
    IF NOT INPUT-FILE-OK
        [code here to check file-status field and crash if bad]
    END-IF
    .

我强烈赞成读底稿。 空文件可能表示问题。现在,您可以进行测试(在读完启动图之后),而不必弄乱您的主要逻辑。您不必在文件末尾跳出循环,因为循环只会用当前记录进入。

I strongly favour the priming-read. An "empty" file may indicate a problem. Now you can test (after the priming read) without having to clutter your main logic. You don't have to "get out of the loop" at end-of-file, because the loop is only ever entered with a current record.

传统上,文件会包含标题(和预告片)。标头将包含日期,逻辑文件名等。标头将被读取并验证以知道正在处理正确的文件。然后,您需要检查是否没有两个标头(因为如果没有,则会有一天)。完成此操作后,您已经拥有第一条数据记录。

Traditionally files would contain "headers" (and "trailers"). The header would contain a date, logical-file-name, etc. The header would be read and verified to know that the correct file was being processed. Then you need to check that there are not two headers (because if you don't, one day there will be). Whilst you've done that, you already have the first data record.

您不想在某种业务逻辑中进行所有操作,也不想使逻辑流程。

You don't want to do all that in amongst some "business" logic or to clutter the flow of the logic.

在输入记录的88上,请注意这在其他COBOL之间是不可传输的。例如,在IBM Mainframe上,除非您的输入是可变长度记录,并且您仅使用APPLY WRITE(显式或隐式地由可怕的编译器选项AWO使用),然后在打开文件之前,在文件关闭之后使用FD访问数据,或文件结束后将导致崩溃(ABEND)。

On the 88 on the input-record, be aware that this is non-transportable across other COBOLs. On an IBM Mainframe, for instance, unless your input is variable-length records and you use APPLY WRITE ONLY (explicitly, or implicitly by the horrible compiler option AWO) then accessing data under an FD before a file is open, after it is closed, or after end-of-file will lead to a crash (ABEND).

这篇关于读取顺序输入文件的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆