Sometimes you need to make some small changes in the contents of a file. Of course it possible to create a brand new file with the new, modified contents but this is not practical. COBOL provides you with an OPEN I-O mode with which you can modify only the required record in a file.
In other words, there is another file opening mode; the I-O mode; and another special REWRITE statement. Suppose that you want to change the surname field of a record in an indexed file (originally "AYFER") into "AYFEROGLU".
The program that you can write could read like
SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT"
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS M-IDNO.
....
FD MYFILE.
01 MYFILE-REC.
02 M-IDNO PIC XXXX.
02 M-NAME PIC X(16).
02 M-SURNAME PIC X(16).
02 M-BIRTHDATE.
03 M-BD-YEAR PIC 9999.
03 M-BD-MONTH PIC 99.
03 M-BD-DAY PIC 99.
...
PROCEDURE DIVISION.
....
OPEN I-O MYFILE.
....
MOVE "X20" TO M-IDNO.
READ MYFILE INVALID KEY PERFORM KEY-ERROR.
*
* TO MAKE SURE THAT WE ARE ON THE CORRECT RECORD
*
IF M-SURNAME NOT = "AYFER" DISPLAY "WRONG RECORD"
DISPLAY"RECORD NOT UPDATED"
CLOSE MYFILE
STOP RUN.
MOVE "AYFEROGLU" TO M-SURNAME.
REWRITE MYFILE-REC.
....
CLOSE MYFILE.
.....
Important : The key field of a record in an indexed file CANNOT be updated. If the programmer has to update the key field, the only solution is to delete the record with the old key value and a add the same record to the file; this time with the new key value.
In other words, there is another file opening mode; the I-O mode; and another special REWRITE statement. Suppose that you want to change the surname field of a record in an indexed file (originally "AYFER") into "AYFEROGLU".
The program that you can write could read like
SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT"
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS M-IDNO.
....
FD MYFILE.
01 MYFILE-REC.
02 M-IDNO PIC XXXX.
02 M-NAME PIC X(16).
02 M-SURNAME PIC X(16).
02 M-BIRTHDATE.
03 M-BD-YEAR PIC 9999.
03 M-BD-MONTH PIC 99.
03 M-BD-DAY PIC 99.
...
PROCEDURE DIVISION.
....
OPEN I-O MYFILE.
....
MOVE "X20" TO M-IDNO.
READ MYFILE INVALID KEY PERFORM KEY-ERROR.
*
* TO MAKE SURE THAT WE ARE ON THE CORRECT RECORD
*
IF M-SURNAME NOT = "AYFER" DISPLAY "WRONG RECORD"
DISPLAY"RECORD NOT UPDATED"
CLOSE MYFILE
STOP RUN.
MOVE "AYFEROGLU" TO M-SURNAME.
REWRITE MYFILE-REC.
....
CLOSE MYFILE.
.....
Important : The key field of a record in an indexed file CANNOT be updated. If the programmer has to update the key field, the only solution is to delete the record with the old key value and a add the same record to the file; this time with the new key value.