Opening optional files

If a program tries to open and read a file that does not exist, normally an error occurs.

About this task

However, there might be times when opening a nonexistent file makes sense. For such cases, code the keyword OPTIONAL in the SELECT clause:


SELECT OPTIONAL file ASSIGN TO filename

Whether a file is available or optional affects OPEN processing, file creation, and the resulting file status key. For example, if you open in EXTEND, I-O, or INPUT mode a nonexistent non-OPTIONAL file, the result is an OPEN error, and file status 35 is returned. If the file is OPTIONAL, however, the same OPEN statement returns file status 05, and, for open modes EXTEND and I-O, creates the file.

Related references  
File status key (COBOL for Linux® on x86 Language Reference)  
OPEN statement notes (COBOL for Linux on x86 Language Reference)