Search code examples
cobolcobol85

COBOL85-File processing


I am reading an input file and writing it to an output file till EOF is reached but the issue with my program is after writing the records in the output file till EOF is reached the output file is containing one record extra compared to the input file i.e if input files contains 10 records then the output file is containing 11 records. And the extra record in output file is same as the last record i.e the the last record is repested twice.

So can you please let me know what is the issue with my code.

My program code.

?SYMBOLS
?INSPECT

 IDENTIFICATION DIVISION.
 PROGRAM-ID. InsertRecords.
 AUTHOR. XYZ.


 ENVIRONMENT DIVISION.
 INPUT-OUTPUT SECTION.
 FILE-CONTROL.
      SELECT StudentRecords
             ASSIGN TO "=MKFIL1"
             ORGANIZATION IS SEQUENTIAL
             ACCESS       IS SEQUENTIAL
             FILE STATUS IS WS-FILE-STATUS.

      SELECT NEWStudentRecords
             ASSIGN TO "=MKFIL2"
             ORGANIZATION IS SEQUENTIAL
             ACCESS       IS SEQUENTIAL
             FILE STATUS IS WS-FILE-STATUS.

 DATA DIVISION.
 FILE SECTION.
 FD StudentRecords.
 01 StudentRecord.
   05 Student              PIC X(431).


 FD NewStudentRecords.
 01 NewStudentRecord        PIC X(431).


 WORKING-STORAGE SECTION.

 01 WS-DETAIL-RECORD          PIC X(431).

 01  EOF                      PIC X(3)  VALUE  "NO".

 01 WS-FILE-STATUS            PIC XX VALUE ZEROES.

 01 WS-SOURCE.
   05 PIC X(16) VALUE X"000102030405060708090A0B0C0D0E0F".
   05 PIC X(16) VALUE X"101112131415161718191A1B1C1D1E1F".
   05 PIC X(16) VALUE X"202122232425262728292A2B2C2D2E2F".
   05 PIC X(16) VALUE X"303132333435363738393A3B3C3D3E3F".
   05 PIC X(16) VALUE X"404142434445464748494A4B4C4D4E4F".
   05 PIC X(16) VALUE X"505152535455565758595A5B5C5D5E5F".
   05 PIC X(16) VALUE X"606162636465666768696A6B6C6D6E6F".
   05 PIC X(16) VALUE X"707172737475767778797A7B7C7D7E7F".
   05 PIC X(16) VALUE X"808182838485868788898A8B8C8D8E8F".
   05 PIC X(16) VALUE X"909192939495969798999A9B9C9D9E9F".
   05 PIC X(16) VALUE X"A0A1A2A3A4A5A6A7A8A9AAABACADAEAF".
   05 PIC X(16) VALUE X"B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF".
   05 PIC X(16) VALUE X"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF".
   05 PIC X(16) VALUE X"D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF".
   05 PIC X(16) VALUE X"E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF".
   05 PIC X(16) VALUE X"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF".

 01 WS-TARGET.
   05 PIC X(16) VALUE X"20202020202020202020202020202020".
   05 PIC X(16) VALUE X"20202020202020202020202020202020".
   05 PIC X(16) VALUE X"202122232425262728292A2B2C2D2E2F".
   05 PIC X(16) VALUE X"303132333435363738393A3B3C3D3E3F".
   05 PIC X(16) VALUE X"404142434445464748494A4B4C4D4E4F".
   05 PIC X(16) VALUE X"505152535455565758595A5B5C5D5E5F".
   05 PIC X(16) VALUE X"606162636465666768696A6B6C6D6E6F".
   05 PIC X(16) VALUE X"707172737475767778797A7B7C7D7E20".
   05 PIC X(16) VALUE X"20202020202020202020202020202020".
   05 PIC X(16) VALUE X"20202020202020202020202020202020".
   05 PIC X(16) VALUE X"20202020202020202020202020202020".
   05 PIC X(16) VALUE X"20202020202020202020202020202020".
   05 PIC X(16) VALUE X"20202020202020202020202020202020".
   05 PIC X(16) VALUE X"20202020202020202020202020202020".
   05 PIC X(16) VALUE X"20202020202020202020202020202020".
   05 PIC X(16) VALUE X"20202020202020202020202020202020".


 PROCEDURE DIVISION.
 BEGIN.
    OPEN INPUT StudentRecords
    OPEN OUTPUT NewStudentRecords

    PERFORM UNTIL EOF = "YES"
    READ StudentRecords
       AT END MOVE "YES" TO EOF
    END-READ
          MOVE Student TO WS-DETAIL-RECORD
          INSPECT WS-DETAIL-RECORD CONVERTING WS-SOURCE TO WS-TARGET
              WRITE NewStudentRecord FROM WS-DETAIL-RECORD
    END-PERFORM
    CLOSE StudentRecords
    CLOSE NewStudentRecords
    STOP RUN.

Solution

  • COCOL does not know that you have reached EOF until you attempt to read past the last record.

    Consequently, when you read the last record, EOF is ((not** set to YES, so the next reocrd is "read". That read fails and EOF is set to YES - but you have already started the loop, so the data in the buffer is written again.

    You would therefore need to gate your processing on the EOF status. Personally, I'd move the processing to another paragraph and use

    PERFORM UNTIL EOF = "YES"
    READ StudentRecords
       AT END MOVE "YES" TO EOF
    END-READ
    IF EOF = "NO" PERFORM NEW-PARAGRAPH
    END-PERFORM