Microsoft KB Archive/44354

Explanation of (NO)OPTIONAL-FILE Directive and OPTIONAL Files

PSS ID Number: Q44354 Article last modified on 04-20-1993

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: An OPTIONAL file in COBOL is one that does not exist before it is OPENed. If an OPTIONAL file does not currently exist, it will always be created when it is OPENed in any mode. This function is not true for files that are NOT OPTIONAL. A table of the conditions that can occur in each case is listed below. There are three things that determine whether a program’s files are OPTIONAL: the (NO)OPTIONAL directive, the (NO)ANS85 directive, and whether the (NOT) OPTIONAL statement is included in the SELECT clause for the file. The result of combining these three is described below.

More Information: Three factors determine whether a file is OPTIONAL: High | 1. The OPTIONAL statement inserted in the SELECT clause priority | for that file causes the files to be OPTIONAL. | | 2. The ANS85 directive causes all files to become NOT | OPTIONAL. | | 3. The OPTIONAL-FILE directive causes all files in the Low | program to become OPTIONAL files. OPTIONAL-FILE is priority | the default. The higher priority factor above completely overrides the lower priority. The following example has all three: $SET ANS85 NOOPTIONAL-FILE FILE-CONTROL. SELECT OPTIONAL MYFILE-DAT ASSIGN TO DISK “MYFILE.DAT”. The above example will be an OPTIONAL file, because the SELECT OPTIONAL code overrides the directives. Consider the following example: $SET ANS85 OPTIONAL-FILE FILE-CONTROL. SELECT MYFILE-DAT ASSIGN TO DISK “MYFILE.DAT”. This example will be a NOT OPTIONAL file, because ANS85 is higher priority and overrides the OPTIONAL-FILE directive. Once you have determined whether the file is OPTIONAL or NOT OPTIONAL, several conditions can occur when you OPEN the file. The following table summarizes these conditions: || OPEN | OPEN | OPEN | OPEN | || INPUT | OUTPUT | I/O | EXTEND | ____________||___________|___________|__________|_________________| ————||———–|———–|———-|—————–| OPTIONAL ||successful |successful |successful| successful | file exists ||contents |contents |READ/WRITE|contents retained| ||can be READ|overwritten|usable | WRITE usable | ____________||___________|___________|__________|_________________| NOT OPTIONAL||successful |successful |successful| successful | file exists ||contents |contents |READ/WRITE|contents retained| ||can be READ|overwritten| usable | WRITE usable | ____________||_______ | _______|__________|_________________| || empty file| empty file|empty file| successful | OPTIONAL || created | created | created |contents retained| file does || READ gives| WRITE |WRITE/READ| WRITE usable | NOT exist ||AT END cond| usable |usable | | ____________||___________|___________|__________|_________________| || run-time |empty file | run-time | run-time error | NOT OPTIONAL|| error 013 | created | error 013| 013 “File not | file does ||”File not | WRITE | “File not| found” * | NOT exist || found&quot; * | usable | found&quot; * | | ____________||___________| __________|__________|_________________| * Note: You can recover from run-time error 013 by including a STATUS IS  in your SELECT clause for this file. The program then returns a file status of 35, which means that you tried to OPEN a NOT OPTIONAL file that doesn’t exist for I/O, EXTEND, or INPUT. You could check for this file status, and take the appropriate action.

Additional reference words: 3.00 3.00a Copyright Microsoft Corporation 1993.