Thursday, September 3, 2009

PROCEDURE DIVISION Considerations

Like all files, Indexed files must be OPENed before they can be processes and CLOSEd when they are not needed anymore.

Indexed files can be opened for :

1. OUTPUT
2. INPUT
3. I-O (Input-Output)

Opening an indexed file for OUTPUT means that the program will only issue WRITE statements to a NON-EXISTING and JUST CREATED file. Therefore, when you open a file for OUTPUT, COBOL assumes that the file does not exist and try to create a new one. IF THE FILE EXISTS, ITS CONTENTS WILL BE CLEARED WHEN OPENED AND ASSUMED TO BE A BRAND NEW FILE INTO WHICH RECORDS WILL BE ADDED. You should be very careful when opening a file for OUTPUT. One small mistake and all your valuable records are lost forever.

Once you open an indexed file in Output mode, you are expected to write records in the INCREASING order of keys. That is, before writing the record with key A98 you should first write all the records with key values alphanumerically less than "A98". Please note that indexed fikle keys are case sensitive; that is the key value "a89" is NOT EQUAL TO "A89"; in fact it is alphanumerically greater than "A89" because the ASCII value of "a" (97) is greater than the ASCII value of "A" (65).

Opening an indexed file for INPUT means that the program will only issue READ statements to an EXISTING file. Therefore, when you open a file for INPUT, COBOL assumes that the file exists and try to access it. IF THE FILE DOES NOT EXIST, AN ERROR MESSAGE WILL BE ISSUED INDICATING THAT THE MENTIONED FILE COULD NOT BE FOUND.

If you have declared the ACCESS MODE to be RANDOM, before each READ statement, you are supposed to move a valid KEY value to the record field variable that is declared as the RECORD KEY.

For instance;

OPEN INPUT MYFILE.

....

MOVE "X23" TO M-IDNO.

READ MYFILE.

Good programmers should take precautions in their program to avoid error messages and subsequent abnormal terminations is an INVALID value for the record key specified before the READ statement.

The INVALID KEY clause handles this in COBOL.

MOVE "2300" TO M-IDNO.

READ MYFILE INVALID KEY PERFORM OUT-OF-RANGE.

The INVALID KEY condition raises if there is no record in the file with key value equal to the specified key value.

If you have declared the ACCESS MODE as SEQUENTIAL, you should use the NEXT clause in the READ statement. Like



READ MYFILE NEXT AT END PEFORM NO-MORE-RECORDS.



When you are finished with a file you must CLOSE it.

CLOSE NEW-FILE.

CLOSE OLD-FILE NEW-FILE.

You can close more than one files with a single CLOSE statement. When a COBOL program terminates, all files mentioned in the program are automatically closed; therefore you will not get an error message for those files you forget to close or do not close at all. Please note that relying on COBOL to close your files is not a proper programming style. Although not absolutely necessary, you should close your files when you are done with them.

REWINDING an indexed file with ACCESS MODE RANDOM is not meaninful. If the ACCESS MODE is SEQUENTIAL, you can CLOSE and than OPEN INPUT again to rewind a sequentialy accessed indexed file.