2

您好,我是 cobol 的初学者,需要一些功课帮助。我正在尝试编写一个在输出上打印地址标签的程序。但在输出中必须有标题、页码和日期。我已经成功地让程序以标签格式打印地址,但似乎无法让标题行(带有页面和日期)显示在其上方。使用我的程序时,会出现一个错误代码,说明我对数据文件的访问模式错误。我不确定这意味着什么。这是我的程序。我删除了日期部分只是为了尝试在地址上方获取标题行。*编辑:我已经为“打印头输出”添加了打开和关闭,但现在它给了我错误代码“文件锁定”任何人都可以对此有所了解。

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

   SELECT LABEL-FILE-IN
   ASSIGN TO 'C0603.DAT'
   ORGANIZATION IS LINE SEQUENTIAL.

   SELECT LABEL-FILE-OUT
   ASSIGN TO 'C0603.RPT'
   ORGANIZATION IS LINE SEQUENTIAL.

   SELECT PRINT-HEADER-OUT
   ASSIGN TO 'C0603.RPT'
   ORGANIZATION IS LINE SEQUENTIAL.

   DATA DIVISION.
   FILE SECTION.


   FD LABEL-FILE-IN.
   01 LABEL-RECORD-IN.
   05 CUST-NAME-IN PIC X(20).
   05 ADDRESS-IN PIC X(20).
   05 CITY-STATE-ZIP-IN PIC X(20).

   FD LABEL-FILE-OUT.
   01 LABEL-RECORD-OUT.
   05 PRINT-LABEL-OUT PIC X(20).

   FD  PRINT-HEADER-OUT.
   01  REPORT-OUT                  PIC X(80).


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

   01  HEADING-LINE1.
       05                          PIC X(40) VALUE SPACES.
       05                          PIC X(12) VALUE
           "MAILING LIST".

   01  DATE-WS.
       05 MONTH-WS                 PIC XX.
       05 YEAR-WS                  PIC XX.

   01  DATE-WS-OUT.
       05                           PIC X(45) VALUE SPACES.
       05  MONTH-WS-OUT              PIC XX.
       05                          VALUE "/".
       05  YEAR-WS-OUT               PIC XX.


             PROCEDURE DIVISION.

   000-MAIN-MODULE.
       PERFORM 100-INITIALIZATION-MODULE.
       PERFORM 200-PROCESS-ONE-RECORD
           UNTIL ARE-THERE-MORE-RECORDS = "NO ".
       PERFORM 900-TERMINATION-MODULE.
       STOP RUN.

   100-INITIALIZATION-MODULE.
       OPEN OUTPUT PRINT-HEADER-OUT
       OPEN INPUT LABEL-FILE-IN
       OPEN OUTPUT LABEL-FILE-OUT
       ACCEPT DATE-WS FROM DATE.
       MOVE MONTH-WS TO MONTH-WS-OUT.
       MOVE YEAR-WS TO YEAR-WS-OUT.
       PERFORM 600-READ-MODULE.
       PERFORM 300-TOP-OF-PAGE-MODULE.

   200-PROCESS-ONE-RECORD.
       MOVE SPACES TO PRINT-LABEL-OUT

       MOVE CUST-NAME-IN TO PRINT-LABEL-OUT
       WRITE LABEL-RECORD-OUT

       MOVE ADDRESS-IN TO PRINT-LABEL-OUT
       WRITE LABEL-RECORD-OUT

       MOVE CITY-STATE-ZIP-IN TO PRINT-LABEL-OUT
       WRITE LABEL-RECORD-OUT


   PERFORM 600-READ-MODULE.

   300-TOP-OF-PAGE-MODULE.
       MOVE HEADING-LINE1 TO REPORT-OUT.
       WRITE REPORT-OUT AFTER ADVANCING 9 LINES.
       MOVE DATE-WS-OUT TO REPORT-OUT.
       WRITE REPORT-OUT AFTER ADVANCING 1 LINES.


   600-READ-MODULE.
       READ LABEL-FILE-IN
            AT END MOVE "NO " TO ARE-THERE-MORE-RECORDS
       END-READ.

   900-TERMINATION-MODULE.
       CLOSE PRINT-HEADER-OUT.
       CLOSE LABEL-FILE-IN.
       CLOSE LABEL-FILE-OUT.
4

2 回答 2

3

我认为您遇到的问题是两者都LABEL-FILE指向HEADER-FILE同一个物理文件('C0603.RPT')。您可以这样做,但一次只能打开其中一个。当您尝试以不同的名称再次打开它时,这是“文件锁定”消息的来源。

执行此操作的典型方法是打开一个文件,但有多个记录定义用于写入。

删除:

    SELECT PRINT-HEADER-OUT
    ASSIGN TO 'C0603.RPT'
    ORGANIZATION IS LINE SEQUENTIAL.

并更改 LABEL-FILE-OUT 的 FD 以包含 Header 记录...

    FD LABEL-FILE-OUT.
    01.
       05 LABEL-BUFFER              PIC X(80).
       05 LABEL-RECORD-OUT REDEFINES LABEL-BUFFER.
          10 PRINT-LABEL-OUT PIC X(20).
          10                 PIC X(60).
       05 PRINT-HEADER-OUT REDEFINES LABEL-BUFFER.
          10 REPORT-OUT      PIC X(80).

还有其他方法可以做到这一点,但基本思想是拥有一个至少与最大输出记录一样大的输出缓冲区,并且REDEFINE可以用于多种用途(标签或标题)。

写入标签行或标题行时,只需WRITE LABEL-BUFFER在每次写入后使用 SPACES 并将其移动到其中,以确保在重新填充任何从属数据项之前正确初始化它。

于 2012-05-03T15:19:34.230 回答
0

“错误代码指出我对数据文件的访问模式错误”是因为执行 WRITE REPORT-OUT 语句时 PRINT-HEADER-OUT 文件未打开。所有文件在使用前都必须打开,并且在使用完后应始终关闭。

于 2012-05-02T21:39:34.160 回答