COBOL File Processing
Processing of a file is nothing but performing any operations like READ/WRITE/REWRITE etc other than OPEN and CLOSE.
The below are the list of file processing operations.
- READ NEXT
- READ PREV
The main purpose of the READ statement is that to read the records from the file.
At a time one record can be READ from a file.
The FILE READ topic explained in detail here
Write Statement is used to write the provided record into the file.
Writing into the file can be done in two ways based on the file opening mode.
1. If the file opened in OUTPUT mode, the records will be writing from the first record onwards. If the file have some data before opening in OUTPUT mode, that can be refreshed and start writing from beginning onwards.
2. If the file opened in EXTEND mode, the records will be appending to the file from the last record onwards.
The FILE WRITE topic explained in detail here
REWRITE is mainly used to update the records which is already existed in the file/already written to the file.
REWRITE can only work when the file is opened in I-O mode.
The FILE REWRITE topic explained in detail here
DELETE is used to delete the record which is read in the latest READ of the file.
To delete the record, file should be opened in I-O mode.
Specific record deletion is not possible in sequential files.
Whenever we open a sequential file in output mode for second time, all the previous data get refreshed (deleted).
The FILE DELETE topic explained in detail here
START is used in DYNAMIC access mode of indexed files.
START is used to set the pointer to the next read for reading the record.
START will be not retrieving any record and it only sets the pointer to the next read for reading the record.
The FILE START topic explained in detail here
File READ NEXT:
READ NEXT is used to the records sequentially after the START statement placed the cursor based on the key provided.
READ NEXT also used in DYNAMIC mode only where the START command used.
The FILE READ NEXT topic explained in detail here
Below example covers two file processing commands
IDENTIFICATION DIVISION. PROGRAM-ID. FILEIO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FILE1 ASSIGN TO DISK1. ORGANIZATION IS INDEXED. ACCESS MODE IS RANDOM. RECORD KEY IS STD-NO. FILE STATUS IS WS-FS. DATA DIVISION. FILE SECTION. FD FILE1. RECORD CONTAINS 80 CHARACTERS. BLOCK CONTAINS 800 CHARACTERS. RECORDING MODE IS F. DATA RECORD IS STD-REC. 01 STD-REC. 02 STD-NO PIC 9(03). 02 STD-NAME PIC X(20). 02 STD-GENDER PIC X(07). 02 FILLER PIC X(50). WORKING-STORAGE SECTION. 77 WS-STD-NO PIC 9(03). 77 WS-STD-NAME PIC X(20). 77 WS-FS PIC 9(02). 01 WS-EOF-SW PIC X(01) VALUE 'N'. 88 EOF-SW VALUE 'Y'. 88 NOT-EOF-SW VALUE 'N'. PROCEDURE DIVISION. ACCEPT WS-STD-NO. ACCEPT WS-STD-NAME. MOVE WS-STD-NO TO STD-NO. DISPLAY 'INDEXED FILE READING...'. OPEN I-O FILE1. READ FILE1 AT END MOVE 'Y' TO WS-EOF-SW. IF WS-FS = 00 MOVE WS-STD-NAME TO STD-NAME REWRITE STD-REC ELSE DISPLAY ' RECORD NOT FOUND IN FILE...' END-IF. CLOSE FILE1. STOP RUN.
COBOL File Open COBOL File Read