COBOL中的open扩展无法正常工作

时间:2013-03-12 12:42:58

标签: file io cobol logfile

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

   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
   . 

2 个答案:

答案 0 :(得分:2)

嗯,这是一个猜测,因为你没有提供太多,但如果“LOGFILE.txt”不存在,你将需要选择SELECT。

建议您在代码中输入FILE STATUS。

答案 1 :(得分: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