1

在下面的程序中,我执行与索引文件的基本交互。当我尝试添加记录时,一切都按预期工作,我什至收到成功消息。但是,当读取它的代码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
4

2 回答 2

3

尝试将 OPTIONAL 添加到 SELECT 短语中。保持打开 IO。

选择可选索引文件分配给“indexedfile.dat”。

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

另外,听从比尔的建议;习惯于在任何 OPEN(或任何访问权限)之后检查 FILE-STATUS。

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

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

于 2013-01-17T16:17:34.243 回答
0

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

有关详细说明:请查看下面的评论!

于 2013-01-17T13:58:26.333 回答