Problem with displaying records from a sequential file

100 views Asked by At

I've been learning COBOL for some time now, and I keep encountering this issue. Specifically, it's about reading records from a sequential file. No matter how hard I try, the program's output either mixes up the records or doesn't display them entirely. Changing the file organization to "line sequential" helps, but what should I change to correctly display data from a sequential file? I'm using GNU COBOL for compilation.

Below is an example program, input file, and program output.

Example cobol program:

       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.

input file:

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

Program output:

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

Thanks in advance for your help.

2

There are 2 answers

0
Rick Smith On BEST ANSWER

When a file is defined as fixed-length sequential, as this one is, the program will read exactly the number of characters defined for the file. However, each record in the file contains an additional two characters. This is evident from the two-character shift in subsequent lines. Those characters are probably carriage return and line feed.

An additional data item must be added to the record description to allow for those characters. I suggest the record description should be —

   01 DataRecord.
       05 RecordField PIC X(40).
       05 CR-LF PIC X(2).
0
Loy On

I don't know about your dev environment but you could try to switch to hexadecimal values and see that's there is something at the end of each records that's not LOW-VALUE (hex 00)