显示连续文件中的记录时出现问题

问题描述 投票:0回答:1

我学习COBOL已经有一段时间了,我一直遇到这个问题。具体来说,它是从顺序文件中读取记录。无论我多么努力,程序的输出要么会混淆记录,要么不会完全显示它们。将文件组织更改为“行顺序”会有所帮助,但是我应该更改什么才能正确显示顺序文件中的数据?我正在使用 GNU COBOL 进行编译。

下面是示例程序、输入文件和程序输出。

cobol 程序示例:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ReadSequentialFile.
       
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT DataFile ASSIGN TO "data.dat"
                   ORGANIZATION IS SEQUENTIAL.
       
       DATA DIVISION.
       FILE SECTION.
       FD DataFile.
       01 DataRecord.
           05 RecordField PIC X(40).
       
       WORKING-STORAGE SECTION.
       01 WS-EOF PIC X VALUE 'N'.

       
       
       PROCEDURE DIVISION.
           OPEN INPUT DataFile.
           PERFORM UNTIL WS-EOF = 'Y'
               READ DataFile
                    AT END
                        MOVE 'Y' TO WS-EOF
                    NOT AT END
                        DISPLAY "REKORD" SPACE RecordField
                       
           END-PERFORM.
           CLOSE DataFile.
           STOP RUN.

输入文件:

1exampleofrecordexampleofrecord123456789
2exampleofrecordexampleofrecord123456789
3exampleofrecordexampleofrecord123456789
4exampleofrecordexampleofrecord123456789
5exampleofrecordexampleofrecord123456789
6exampleofrecordexampleofrecord123456789
7exampleofrecordexampleofrecord123456789
8exampleofrecordexampleofrecord123456789
9exampleofrecordexampleofrecord123456789
0exampleofrecordexampleofrecord123456789
1exampleofrecordexampleofrecord123456789

程序输出:

REKORD 1exampleofrecordexampleofrecord123456789
REKORD
2exampleofrecordexampleofrecord1234567
REKORD 89
3exampleofrecordexampleofrecord12345
REKORD 6789
4exampleofrecordexampleofrecord123
REKORD 456789
5exampleofrecordexampleofrecord1
REKORD 23456789
6exampleofrecordexampleofrecor
REKORD d123456789
7exampleofrecordexampleofrec
REKORD ord123456789
8exampleofrecordexampleofr
REKORD ecord123456789
9exampleofrecordexampleo
REKORD frecord123456789
0exampleofrecordexampl
REKORD eofrecord123456789
1exampleofrecordexam
REKORD pleofrecord1234567891exampleofrecordexam

预先感谢您的帮助。

cobol mainframe gnucobol
1个回答
0
投票

当文件被定义为固定长度顺序文件时,就像这个文件一样,程序将准确读取为文件定义的字符数。但是,文件中的每条记录都包含额外的两个字符。这从随后几行的两个字符的转变中可以明显看出。这些字符可能是回车符和换行符。

必须将附加数据项添加到记录描述中以允许使用这些字符。我建议记录描述应该是 —

   01 DataRecord.
       05 RecordField PIC X(40).
       05 CR-LF PIC X(2).
© www.soinside.com 2019 - 2024. All rights reserved.