Like all files, sequential and line sequential files must be OPENed before they can be processes and CLOSEd when they are not needed anymore.
Seq and Line Seq files can be opened for :
1. OUTPUT
2. INPUT
3. EXTEND
4. I-O (Input-Output)
Opening a seq 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.
Opening a seq 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.
Opening a seq file for EXTEND means that the program will add NEW RECORDS to an EXISTING file. Therefore, when you open a file for EXTEND, COBOL assumes that the file exists and subsequent WRITE statements will try to ADD NEW RECORDS at the end of the existing file (in other words; the append mode). Records can only be added to the END of a sequential file.
Some examples are :
OPEN OUTPUT NEW-FILE.
OPEN INPUT OLD-FILE.
OPEN EXTEND OLD-FILE OLD-FILE2.
OPEN INPUT OLD-FILE OUTPUT NEW-FILE.
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.
The CLOSE statement has a very important function. Sometimes, your program needs to re-start reading a seq file from the beginning. (Please note that every READ statement you execute, brings the next record into memory and when you skip a record, you cannot backspace the file to previous records.) In such a situation, you will have to REWIND the file, that is make the first record the active one in memory. You can achieve this only by closing the file and re-opening it for INPUT.
You should be careful not to open an already opened file. Closing already closed files usually do not cause any problems but should be avoided. You should make sure that your program opens and closes files at proper places in your logic flow so that no input-output is tried on closed files.
Seq and Line Seq files can be opened for :
1. OUTPUT
2. INPUT
3. EXTEND
4. I-O (Input-Output)
Opening a seq 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.
Opening a seq 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.
Opening a seq file for EXTEND means that the program will add NEW RECORDS to an EXISTING file. Therefore, when you open a file for EXTEND, COBOL assumes that the file exists and subsequent WRITE statements will try to ADD NEW RECORDS at the end of the existing file (in other words; the append mode). Records can only be added to the END of a sequential file.
Some examples are :
OPEN OUTPUT NEW-FILE.
OPEN INPUT OLD-FILE.
OPEN EXTEND OLD-FILE OLD-FILE2.
OPEN INPUT OLD-FILE OUTPUT NEW-FILE.
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.
The CLOSE statement has a very important function. Sometimes, your program needs to re-start reading a seq file from the beginning. (Please note that every READ statement you execute, brings the next record into memory and when you skip a record, you cannot backspace the file to previous records.) In such a situation, you will have to REWIND the file, that is make the first record the active one in memory. You can achieve this only by closing the file and re-opening it for INPUT.
You should be careful not to open an already opened file. Closing already closed files usually do not cause any problems but should be avoided. You should make sure that your program opens and closes files at proper places in your logic flow so that no input-output is tried on closed files.