We should open a file before we perform any operation in it. In COBOL, ‘OPEN’ is used to open a file. After the successful open of the program, it can perform READ/WRITE/REWRITE/UPDATE/DELETE functionality.
In the end, we should close using CLOSE operation. After successful execution of the program, all the files used are automatically closed but it is always a good practice to close the file to avoid any issue.
Let us see all the file operations-
OPEN File in COBOL
All the files must open in PROCEDURE DIVISION before any other operations are performed in it.
Syntax
OPEN INPUT FILE-NAME.
– File opened for Reading
OPEN OUTPUT FILE-NAME.
– File opened for Writing
OPEN I-O FILE-NAME.
– File opened for reading as well as writing (used mostly for updating of the record in the file)
OPEN EXTEND FILE-NAME.
– File opened for appending. Used for Sequential Files.
READ File in COBOL
All the input files must be READ in PROCEDURE DIVISION before the records in the file we use further. If we want to read the content of any type of file, we have to use READ operation.
The Syntax of READING Format/Example 1 for Sequential Files
READ FILE-NAME [NEXT/PREVIOUS] RECORD [INTO identifier1]
[AT END {imperative statement}]
[NOT AT END {imperative statement}]
[END-READ]
NEXT – This we should use for DYNAMIC Access mode for sequential files.
READ FILE-NAME RECORD [INTO identifier1][KEY IS key-1]
[INVALID KEY {imperative statement}]
[NOT INVALID KEY {imperative statement}]
[AT END {imperative statement}]
[NOT AT END {imperative statement}]
[END-READ]
Syntax of READ Format/Example 2 for Sequential Files
START File in COBOL
- If we want to position the pointer at a specific position in INDEXED or RELATIVE ORGANIZATION, we have to use START operation.
- We can only use START if the file is opened in I-O mode.
- We can only use START when access mode is in sequential or Dynamic.
Syntax of START
START FILE-NAME
[KEY IS { =/EQUAL TO/GREATER THAN/>/LESS THAN/</NOT LESS THAN/NOT < THAN.. } key-1]
[INVALID KEY {imperative statement}]
[NOT INVALID KEY {imperative statement}]
[END-START]
WRITE File in COBOL
We use the WRITE operation to write the content to a file. To write to a file, the file should be opened in OUTPUT mode.
Syntax of WRITE
WRITE FILE-RECORD [FROM identifier1/literal1]
[BEFORE/AFTER ADVANCING {Identifier2/number2/PAGE} [LINE/LINES]
[KEY IS key-1]
[INVALID KEY {imperative statement}]
[NOT INVALID KEY {imperative statement}]
[AT END-OF-PAGE/EOP {imperative statement}]
[NOT AT END-OF-PAGE/EOP {imperative statement}]
[END-WRITE]
Here,
FROM – This is optional.
BEFORE ADVANCING option – This we use only for Reports where the line must be printed before the next page is advanced.
AFTER ADVANCING option – This we use only for Reports where the line must be printed after next page is advanced.
END-WRITE is also optional.
Most used WRITE Format/Example 1
WRITE FILE-RECORD.
Most used WRITE Format/Example 2
MOVE WS-DATA TO WS-REC
WRITE FILE-REC FROM WS-REC
INVALID KEY DISPLAY ‘INVALID KEY: ’
NOT INVALID KEY DISPLAY ‘VALID KEY: ’
END-WRITE.
MOVE WS-DATA TO WS-REC ==>>This can be done in some other section as well so that WS-REC is populated with a value.
REWRITE
We use REWRITE operation to update record/s in a file
The File must be opened in I-O mode
Syntax of REWRITE
REWRITE FILE-RECORD [FROM identifier1/literal1]
[INVALID KEY {imperative statement}]
[NOT INVALID KEY {imperative statement}]
[END-REWRITE]
DELETE
Use DELETE operation to delete record/s from a file
The File must be opened in I-O mode and the record which is last read is deleted one by one.
Syntax of DELETE
DELETE FILE-NAME FILE-RECORD
[INVALID KEY {imperative statement}]
[NOT INVALID KEY {imperative statement}]
[END-DELETE]
CLOSE
We use a CLOSE operation to close a file explicitly
Syntax of CLOSE
CLOSE FILE-NAME