A Cobol program reads a record from a first flat file and compares it to the first record on a second flat file. However, because the first record from the first flat file does not match any records on the second flat file, the Cobol program goes into an infinite loop. How do I fix it?
Share
Smells like a logic error somewhere in the program. Hard to say
what that might be. But I do have a few ideas…
Possible causes of an infinite loop:
End of file is sometimes determined by testing the File Status after each
I/O operation. File Status is an optional 2 character data item associated with the file being
read/written. It is specified
in the FILE-CONTROL paragraph of your program. For example:
where:
file-nameis the name you refer to in OPEN/READ/WRITE/CLOSE statements.dd-nameis the external file name (the DDNAME from your JCL).
fstatusis a two character data item declaredunder WORKING-STORAGE.
The File Status is set on every file I/O operation. For example:
sets the
fstatusto end-of-file if there were no more records to read. Note that the File Statusvariable is not actually referenced on the
READ, but it is set.File Status values are two characters and are defined in the ISO COBOL standard, they should
be the same for all COBOL implementations. The exception being File Status values where the first
character is a ‘9’, these are implementation dependant. Here is a link to the IBM Enterprise
COBOL File Status values
The value for end-of-file is: ’10’ – which should be the same for all COBOL implementations.
It is my guess that your program has a File Status for each of the input files, but is not checking it or reacting to it
appropriately. For example, your program may only check for end-of-file but not other conditions:
The problem with this approach is that it treats normal returns (fstatus = ’00’) and all non-end-of-file error
conditions as if the READ was successful. Better to have something like:
There is an imperative form of the
READstatement that specifies what to do when the end of fileis reached. It goes something like:
Again, if a File Status was specified in the FILE-CONTROL section for
file-nameand a non-end-of-fileerror occurred, your program would attempt to continue with ‘normal’ logic – exactly the wrong thing to be
doing.