写入索引文件会返回成功但失败

时间:2013-01-17 07:59:22

标签: input indexing cobol

在以下程序中,我执行与索引文件的基本交互。当我尝试添加记录时,一切都按预期工作,我甚至收到成功消息。但是当读取它的代码 - lees - 试图找到相应的ID时,它找不到它。当我查看磁盘上索引文件的大​​小时,我可以看到它的大小为0kb。我在俯瞰什么?

000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. oef16_indexedfiles.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT indexfile ASSIGN TO "C:\Test\indexedfile.dat"
ORGANIZATION IS INDEXED
ACCESS IS DYNAMIC
RECORD KEY IS id-number
ALTERNATE KEY IS fname WITH DUPLICATES
ALTERNATE KEY IS lname WITH DUPLICATES
ALTERNATE KEY IS city WITH DUPLICATES
ALTERNATE KEY IS hobby WITH DUPLICATES.

DATA DIVISION.
FILE SECTION.
FD indexfile.
01 persoon.
 02 id-number    PIC 9.
 02 name.
        03 fname        PIC A(25).
        03 lname        PIC A(30).
 02 city         PIC A(30).
 02 hobby        PIC X(40).

WORKING-STORAGE SECTION.
01 einde        PIC X.
01 msg          PIC X(50).
01 countno      PIC 9 VALUE 1.

PROCEDURE DIVISION.
PGM.
 PERFORM schrijf
 PERFORM lees
 STOP RUN.

   check.
    DISPLAY msg
    ACCEPT einde

   schrijf.
   MOVE "1 = stoppen, 0 = doorgaan" TO msg       
   PERFORM check
   OPEN I-O indexfile

   PERFORM UNTIL einde = 1
    MOVE countno TO id-number

    DISPLAY "Enter your first name:"
    ACCEPT fname

    DISPLAY "Enter your last name:"
    ACCEPT lname

    DISPLAY "Enter your city:"
    ACCEPT city

    DISPLAY "Enter your hobby:"
    ACCEPT hobby

    WRITE persoon
            INVALID KEY
                    DISPLAY "ERROR WRITING: " id-number
            NOT INVALID KEY
                    DISPLAY "ID " id-number " by " fname " is added"
    END-WRITE
    ADD 1 TO countno

    PERFORM check
   END-PERFORM
   CLOSE indexfile
   MOVE 0 TO einde

   lees.
    OPEN INPUT indexfile
    MOVE 1 TO id-number
    READ indexfile 
            INVALID KEY
                    DISPLAY "Invalid key: " id-number
            NOT INVALID KEY
                    DISPLAY id-number SPACE fname SPACE lname SPACE
                            city SPACE hobby
    END-READ

    CLOSE indexfile
    MOVE 0 TO einde
    ACCEPT einde

2 个答案:

答案 0 :(得分:3)

尝试在SELECT短语中添加OPTIONAL。保持OPEN I-O。

SELECT 可选索引文件ASSIGN TO“indexedfile.dat”。

OPTIONAL将允许打开一个不存在的文件,为第一次写入做准备。

另外,请遵照比尔的建议;习惯在任何OPEN(或任何访问)之后检查FILE-STATUS。

在句点终止你的段落,并重新格式化为FIXED表格(一些显示行超过第72列)后,一旦添加了OPTIONAL,这段代码就可以正常使用OpenCOBOL。在此之前,它失败了

libcob:文件不存在(STATUS = 35)文件:'indexedfile.dat'

答案 1 :(得分:0)

我被其他人指出了答案。在write方法(schrijf)中,打开文件时必须使用OUTPUT方法而不是I-O

详细说明:请查看以下评论​​!