Thursday, September 3, 2009

UPDATING RECORDS OF A RELATIVE FILE

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 the 20th record of a relative file (originally "AYFER") into "AYFEROGLU".

The program that you can write could read like

SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT"

ORGANIZATION IS RELATIVE

ACCESS MODE IS RANDOM

RECORD KEY IS M-IDNO.

....

FD MYFILE.

01 MYFILE-REC.

02 M-IDNO PIC 9999.

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 20 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.

.....