打开文件时收到“不允许写”错误

时间:2019-04-15 22:29:36

标签: cobol

运行下面的代码时,该代码将输出所有基于输入文件获得奖金的人的报告,我在ln 159上收到错误消息“不允许写,文件未打开以供输出(状态= 48)”。 / p>

我曾尝试在500-HEADING模块中打开文件,但是收到一条错误消息:“文件已打开(状态= 41)”,并且在将OPEN语句移至100-MAIN模块时收到原始错误。使用I-O,INPUT-OUTPUT和EXTEND返回语法错误。

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ASSIGNMENT9_1_SORT.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT PAYROLL-MASTER ASSIGN TO 'CH0901-UNSORTED.DAT'
           ORGANIZATION IS LINE SEQUENTIAL.
           SELECT WORK-FILE ASSIGN TO 'TEMP.DAT'
           ORGANIZATION IS LINE SEQUENTIAL.
           SELECT BONUS-REPORT ASSIGN TO 'BONUS REPORT SORTED.LST'
           ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       FILE SECTION.
       FD  PAYROLL-MASTER.
       01  PAYROLL-REC.
           05 EMP-NO-IN                 PIC X(5).
           05 NAME-IN                   PIC X(20).
           05 TERRITORY-NO-IN           PIC XX.
           05 OFFICE-NO-IN              PIC XX.
           05 ANNUAL-SALARY-IN          PIC 9(6).
           05                           PIC X(29).
           05 DATE-HIRED-IN.
                10 MONTH-IN             PIC 99.
                10 DAY-IN               PIC 99.
                10 YEAR-IN              PIC 9999.
           05                           PIC X(10).
       FD  BONUS-REPORT.
       01  BONUS-PRINT                  PIC X(80).
       SD  WORK-FILE.
       01  SORT-REC.
           05 EMP-NO                    PIC X(5).
           05 S-NAME                    PIC X(20).
           05 TERR                      PIC XX.
           05 OFFICE                    PIC XX.
           05 ANNUAL-SALARY             PIC 9(6).
           05                           PIC X(29).
           05 DATE-HIRED.
                10 S-MONTH              PIC 99.
                10 S-DAY                PIC 99.
                10 S-YEAR               PIC 9999.
           05                           PIC X(10).
       WORKING-STORAGE SECTION.
       01  BONUS-REC.
           05 EMP-NO-OUT                PIC X(5).
           05 NAME-OUT                  PIC X(20).
           05 TERRITORY-NO-OUT          PIC XX.
           05 OFFICE-NO-OUT             PIC XX.
           05 ANNUAL-SALARY-OUT         PIC 9(6).
           05                           PIC X(29).
           05 DATE-HIRED-OUT.
               10 MONTH-OUT             PIC 99.
               10 DAY-OUT               PIC 99.
               10 YEAR-OUT              PIC 9999.
           05                           PIC X(10).
       01  WS-DATE.
           05 WS-YEAR                   PIC 9999.
           05 WS-MONTH                  PIC 99.
           05 WS-DAY                    PIC 99.
       01  HEADER1.
           05                           PIC X(40) VALUE SPACES.
           05                           PIC X(15) VALUE 'BONUS REPORT'.
           05                           PIC X(5) VALUE SPACES.
           05                           PIC X(5) VALUE 'PAGE '.
           05 PAGE-NO                   PIC 99 VALUE 1.
           05                           PIC XXX.
           05 TODAYS-DATE.
              10 MONTH-FIELD            PIC 99.
              10                        PIC X VALUE '/'.
              10 DAY-FIELD              PIC XX.
              10                        PIC X VALUE '/'.
              10 YEAR-FIELD             PIC 9999.
       01  BLANK-LINE                          PIC X(80).
       01  FIRST-RECORD                 PIC X VALUE 'Y'.
       01  HEADER2.
           05                           PIC X(10) VALUE SPACES.
           05                           PIC X(13) VALUE 'TERRITORY --'.
           05 TERRITORY-NO              PIC 99.
       01  HEADER3.
           05                           PIC X(20) VALUE SPACES.
           05                           PIC X(10) VALUE 'OFFICE --'.
           05 OFFICE-NO                 PIC 99.
       01  HEADER4.
           05                           PIC X(10) VALUE SPACES.
           05                           PIC X(15) VALUE 'EMPLOYEE NAME'.
           05                           PIC X(5) VALUE SPACES.
           05                           PIC X(5) VALUE 'BONUS'.
       01  DATA1.
           05                           PIC X(6) VALUE SPACES.
           05 NAME                      PIC X(20).
           05                           PIC XXXX.
           05 BONUS                     PIC $ZZ,ZZZ.99 VALUE ZEROES.
       01  ARE-THERE-MORE-RECORDS       PIC X VALUE 'Y'.
       01  LINE-COUNT                   PIC 99 VALUE 1.

       PROCEDURE DIVISION.
       100-MAIN.
           SORT WORK-FILE
             ON ASCENDING KEY TERR
             ON ASCENDING KEY OFFICE
             USING PAYROLL-MASTER
             GIVING BONUS-REPORT
           PERFORM 200-INPUT
           STOP RUN.

       200-INPUT.
           OPEN INPUT BONUS-REPORT
           MOVE FUNCTION CURRENT-DATE TO WS-DATE
           MOVE WS-YEAR TO YEAR-FIELD
           MOVE WS-MONTH TO MONTH-FIELD
           MOVE WS-DAY TO DAY-FIELD
           PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'N'
                READ BONUS-REPORT NEXT RECORD
                   AT END
                      MOVE 'N' TO ARE-THERE-MORE-RECORDS
                   NOT AT END
                      PERFORM 300-MOVE
                      PERFORM 400-CALC
                END-READ
           CLOSE BONUS-REPORT
           END-PERFORM.

       300-MOVE.
           MOVE EMP-NO-IN TO EMP-NO-OUT
           MOVE NAME-IN TO NAME-OUT
           MOVE TERRITORY-NO-IN TO TERRITORY-NO-OUT
           MOVE OFFICE-NO-IN TO OFFICE-NO-OUT
           MOVE ANNUAL-SALARY-IN TO ANNUAL-SALARY-OUT
           MOVE DATE-HIRED-IN TO DATE-HIRED-OUT.

       400-CALC.
           MOVE ZEROES TO BONUS
           IF YEAR-OUT IS LESS THAN 1994
               COMPUTE BONUS = ANNUAL-SALARY-OUT * 0.10
           END-IF
           EVALUATE TRUE
             WHEN FIRST-RECORD = 'Y'
                MOVE TERRITORY-NO-OUT TO TERRITORY-NO
                MOVE OFFICE-NO-OUT TO OFFICE-NO
                PERFORM 500-HEADING
                MOVE NAME-OUT TO NAME
                WRITE BONUS-PRINT FROM DATA1
                ADD 1 TO LINE-COUNT
                MOVE 'N' TO FIRST-RECORD
             WHEN FIRST-RECORD = 'N'
                MOVE TERRITORY-NO-OUT TO TERRITORY-NO
                MOVE OFFICE-NO-OUT TO OFFICE-NO
                MOVE NAME-OUT TO NAME
                IF LINE-COUNT > 10
                   MOVE 1 TO LINE-COUNT
                   ADD 1 TO PAGE-NO
                   PERFORM 500-HEADING
                END-IF
           WRITE BONUS-PRINT FROM DATA1
           ADD 1 TO LINE-COUNT
           END-EVALUATE.

       500-HEADING.
           WRITE BONUS-PRINT FROM BLANK-LINE
           WRITE BONUS-PRINT FROM HEADER1
           WRITE BONUS-PRINT FROM BLANK-LINE
           WRITE BONUS-PRINT FROM HEADER2
           WRITE BONUS-PRINT FROM BLANK-LINE
           WRITE BONUS-PRINT FROM HEADER3
           WRITE BONUS-PRINT FROM BLANK-LINE
           WRITE BONUS-PRINT FROM BLANK-LINE
           WRITE BONUS-PRINT FROM HEADER4.
`

2 个答案:

答案 0 :(得分:2)

更改

       OPEN INPUT BONUS-REPORT

收件人

       OPEN OUTPUT BONUS-REPORT

进一步检查发现附加错误。

SORT语句应该创建了另一个文件作为输入,而不是您打算用于输出的文件。或者,正如@Jim Castro暗示的另一个答案一样,使用OUTPUT PROCEDURE访问记录。以下区别显示了附加文件以及该文件的OPENCLOSE语句。

此外,还需要从CLOSE中删除PERFORM语句。 @Jim Castro也注意到了。

最后,END-EVALUATE必须放在WRITE语句之前。

以下是识别更改的差异实用程序的输出。 OLD 是您发布的程序, NEW 是我应用更改后的程序。

* Text File Comparison
*-------------------------------------------------------------------------------
* 000008/000008: mis-matched records
* OLD :-
           SELECT WORK-FILE ASSIGN TO 'TEMP.DAT'
           ORGANIZATION IS LINE SEQUENTIAL.
* NEW :-
           SELECT WORK-FILE ASSIGN TO 'TEMP.DAT'.
           SELECT TEMP-BONUS-FILE ASSIGN TO "TEMP-BONUS.DAT"
           ORGANIZATION LINE SEQUENTIAL.
*-------------------------------------------------------------------------------
* 000027/000028: extra NEW records :-
       FD  TEMP-BONUS-FILE.
       01  TEMP-BONUS-REC.
           05 T-EMP-NO                  PIC X(5).
           05 T-NAME                    PIC X(20).
           05 T-TERR                    PIC XX.
           05 T-OFFICE                  PIC XX.
           05 T-ANNUAL-SALARY           PIC 9(6).
           05                           PIC X(29).
           05 T-DATE-HIRED.
                10 T-MONTH              PIC 99.
                10 T-DAY                PIC 99.
                10 T-YEAR               PIC 9999.
           05                           PIC X(10).
*-------------------------------------------------------------------------------
* 000102/000116: mis-matched records
* OLD :-
             GIVING BONUS-REPORT
* NEW :-
             GIVING TEMP-BONUS-FILE
*-------------------------------------------------------------------------------
* 000107/000121: mis-matched records
* OLD :-
           OPEN INPUT BONUS-REPORT
* NEW :-
           OPEN INPUT TEMP-BONUS-FILE
               OUTPUT BONUS-REPORT
*-------------------------------------------------------------------------------
* 000113/000128: mis-matched records
* OLD :-
                READ BONUS-REPORT NEXT RECORD
* NEW :-
                READ TEMP-BONUS-FILE NEXT RECORD
*-------------------------------------------------------------------------------
* 000120/000135: mis-matched records
* OLD :-
           CLOSE BONUS-REPORT
           END-PERFORM.

       300-MOVE.
           MOVE EMP-NO-IN TO EMP-NO-OUT
           MOVE NAME-IN TO NAME-OUT
           MOVE TERRITORY-NO-IN TO TERRITORY-NO-OUT
           MOVE OFFICE-NO-IN TO OFFICE-NO-OUT
           MOVE ANNUAL-SALARY-IN TO ANNUAL-SALARY-OUT
           MOVE DATE-HIRED-IN TO DATE-HIRED-OUT.
* NEW :-
           END-PERFORM
           CLOSE BONUS-REPORT TEMP-BONUS-FILE.

       300-MOVE.
           MOVE T-EMP-NO TO EMP-NO-OUT
           MOVE T-NAME TO NAME-OUT
           MOVE T-TERR TO TERRITORY-NO-OUT
           MOVE T-OFFICE TO OFFICE-NO-OUT
           MOVE T-ANNUAL-SALARY TO ANNUAL-SALARY-OUT
           MOVE T-DATE-HIRED TO DATE-HIRED-OUT.
*-------------------------------------------------------------------------------
* 000154/000169: mis-matched records
* OLD :-
           WRITE BONUS-PRINT FROM DATA1
           ADD 1 TO LINE-COUNT
           END-EVALUATE.
* NEW :-
           END-EVALUATE
           WRITE BONUS-PRINT FROM DATA1
           ADD 1 TO LINE-COUNT.
*-------------------------------------------------------------------------------

我应该补充一点,可以在300-MOVEINTO BONUS-REC语句上使用READ来替换RETURN段;就像

    READ TEMP-BONUS-FILE NEXT RECORD INTO BONUS-REC

    RETURN SORT-REC INTO BONUS-REC

答案 1 :(得分:2)

很好的第一次尝试,但是您做错了很多事情。如果BONUS-REPORT是程序的最终格式化输出,则不能将其作为输入打开。有些文件是I-O,但是这里不是这种情况。

我建议将SORT动词与OUTPUT PROCEDURE IS语句一起使用。返回SORT-REC,然后格式化并写入奖金报告。您必须更改300和400段落才能移动正确的字段,但最终应该对您有用。

   PROCEDURE DIVISION.
   100-MAIN.

       SORT WORK-FILE
         ON ASCENDING KEY TERR
         ON ASCENDING KEY OFFICE
         USING PAYROLL-MASTER
         OUTPUT PROCEDURE IS PERFORM 200-OUTPUT

       GOBACK.

   200-OUTPUT.
       OPEN OUTPUT BONUS-REPORT
       MOVE FUNCTION CURRENT-DATE TO WS-DATE
       MOVE WS-YEAR TO YEAR-FIELD
       MOVE WS-MONTH TO MONTH-FIELD
       MOVE WS-DAY TO DAY-FIELD

       PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'N'
             RETURN SORT-REC INTO BONUS-REC              
               AT END
                  MOVE 'N' TO ARE-THERE-MORE-RECORDS
               NOT AT END
                  PERFORM 400-CALC
            END-RETURN
       END-PERFORM.

       CLOSE BONUS-REPORT.

请注意:将BONUS-REPORT的关闭位置移出PERFORM循环。