Saturday, December 6, 2008

Handling an empty input file

When reading a sequential file, I always use the AT END clause. For example:

READ STUDENT-FILE
     AT END MOVE 1 TO END-OF-FILE
END READ

So that if an end of file is reached, END-OF-FILE data field will have a value of '1' and I will use this value to check if an end of file is reached. It also can be used to check for an empty file. It looks like this:

IF END-OF-FILE = 1
     CLOSE STUDENT-FILE
     STOP RUN
END-IF

After the READ statement, before processing the records in the file, I always check if I'm reading an empty file so as to avoid errors and also to save time by ending the program immediately after it satisfies the condition 'IF END-OF-FILE = 1'.