Like all files, relative files must be OPENed before they can be processes and CLOSEd when they are not needed anymore.
Relative files can be opened for :
1. OUTPUT
2. INPUT
3. I-O (Input-Output)
Opening a relative 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 a relative file in Output mode, you are expected to write records in the INCREASING and CONSECUTIVE order of keys. That is, before writing the record with key 98 (98th record) you should first write the first 97 records(.
Opening a relative 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 23 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 the value in M-IDNO is zero, negative or has a value greater than the total number of records in the file.
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.
In order to rewind a relative file with ACCESS MODE RANDOM, you do not need to rewind it when you need to go the first record. Just move 1 to the record key and issue a READ statement.
Relative files can be opened for :
1. OUTPUT
2. INPUT
3. I-O (Input-Output)
Opening a relative 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 a relative file in Output mode, you are expected to write records in the INCREASING and CONSECUTIVE order of keys. That is, before writing the record with key 98 (98th record) you should first write the first 97 records(.
Opening a relative 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 23 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 the value in M-IDNO is zero, negative or has a value greater than the total number of records in the file.
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.
In order to rewind a relative file with ACCESS MODE RANDOM, you do not need to rewind it when you need to go the first record. Just move 1 to the record key and issue a READ statement.