0

我正在尝试在 COBOL 中编写一个子程序来创建一个从我的主程序调用的日志文件。我不希望每次调用子程序时都清除日志文件,所以我使用“打开扩展”。问题是由于未知原因它不起作用,程序什么也不做。当我将“打开扩展”更改为“打开输出”时,它可以工作,但我不希望每次都覆盖文件。

   IDENTIFICATION DIVISION.
   PROGRAM-ID. LOGGER.
   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
   SELECT LOGFILE ASSIGN TO "LOGFILE.txt"
   ORGANIZATION IS LINE SEQUENTIAL.
   DATA DIVISION.
   FILE SECTION.
   FD LOGFILE.
   copy "FDLOGGER.cpy".
   WORKING-STORAGE SECTION.

   LINKAGE SECTION.
   01 LOGFILEBOODSCHAP      PIC X(150) value spaces.

   Procedure division USING LOGFILEBOODSCHAP.
   pgm.
   open EXTEND LOGFILE
   Move Current-time to TIJD
   Move Current-date to DATUM
   Move LOGFILEBOODSCHAP to BOODSCHAP   
   write logrecord
   close LOGFILE
   exit program
   . 
4

2 回答 2

2

好吧,这是一个猜测,因为您提供的内容不多,但如果“LOGFILE.txt”不存在,您的 SELECT 将需要 OPTIONAL。

建议您将文件状态检查放入您的代码中。

于 2013-03-12T14:00:10.373 回答
1

我知道你的问题在这里很老了。但是我今天仍然遇到同样的问题,几个小时后,我发现我正在使用的带有 OpenCobolIDE 4.7.6 的 GnuCOBOL 2.0 在尝试使用 OPEN EXTEND 时会生成错误 35。我以一种不太优雅的方式解决了它,但效果很好。我正在分享我找到的解决方案。

   IDENTIFICATION DIVISION.
   PROGRAM-ID. CREATFIL.

   ENVIRONMENT DIVISION.
   CONFIGURATION SECTION.
   REPOSITORY.
       FUNCTION ALL INTRINSIC.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
       SELECT FD-VIRTUAL ASSIGN TO "CADFILE.TXT"
       ORGANIZATION IS LINE SEQUENTIAL
       ACCESS MODE IS SEQUENTIAL
       FILE STATUS IS WS-GET-ERROR.

   DATA DIVISION.
   FILE SECTION.
   FD  FD-VIRTUAL.
   01  FS-BOOK.
       05 NOME PIC X(40).

   WORKING-STORAGE SECTION.
   77  WS-GET-ERROR      PIC XX.

   LOCAL-STORAGE SECTION.
   01  TB-VIRTUAL.
       05  TB-BOOK.
           10 LS-NOME    PIC X(40).
   77  LS-ENTER          PIC X.
   77  WS-RESP           PIC A VALUE "Y".

   PROCEDURE DIVISION.
       OPEN EXTEND FD-VIRTUAL.
       IF WS-GET-ERROR = "35" 
          OPEN OUTPUT FD-VIRTUAL
       END-IF
       IF WS-GET-ERROR = "00"
          PERFORM UNTIL UPPER-CASE(WS-RESP) NOT = "Y"
             DISPLAY X"0D"
             DISPLAY "Name: " WITH NO ADVANCING
             ACCEPT LS-NOME
             WRITE FS-BOOK FROM TB-BOOK
             DISPLAY "More record (Y) for YES " WITH NO ADVANCING
             DISPLAY "- any key for NOT: " 
                WITH NO ADVANCING
             ACCEPT WS-RESP
             IF UPPER-CASE(WS-RESP) NOT = "Y"
                EXIT PERFORM
             END-IF
          END-PERFORM
       END-IF.

       CLOSE FD-VIRTUAL.

       DISPLAY X"0D".
       DISPLAY "Press <ENTER> to finish... " WITH NO ADVANCING.
       ACCEPT LS-ENTER.
       STOP RUN.
   END PROGRAM CREATFIL.

遵守指示

OPEN EXTEND FD-VIRTUAL.
IF WS-GET-ERROR = "35" 
   OPEN OUTPUT FD-VIRTUAL
END-IF
于 2020-06-26T22:24:58.350 回答