REWRITE, DELETE, and EXTEND
In order to ammend a record in a file, such as to update
data (see League Table Program
in sample programs section), to delete a record altogther, or to add a record to
the end of a file, you can use REWRITE, DELETE or EXTEND,
A D V E R T I S E M E N T
However, to use REWRITE or DELETE you must open the file using I-O
mode. Also, DELETE can only be used on files with RELATIVE or INDEXED
organization (see example below).
RELATIVE and INDEXED files are discussed in the following
The format of the DELETE statement is:
ON INVALID KEY
NOT ON INVALID KEY
ON INVALID KEY means the record was not found, so
you might want to display an error message
e.g. DISPLAY 'RECORD NOT FOUND'
To REWRITE you can refer to the level 01 name to change
the record with the ammended field:
03 IN-NAME PIC X(20).
03 IN-ADDRESS PIC X(60).
OPEN I-O IN-FILE
IF IN-NAME = 'BILLY NOMATES' THEN
MOVE 'JIMMY MOREPALS' TO IN-NAME
To EXTEND you must open the file in EXTEND mode:
OPEN EXTEND IN-FILE
DISPLAY 'Type in new name'
MOVE NEW-NAME TO IN-NAME
DISPLAY 'Type in new address'
MOVE NEW-ADDRESS TO IN-ADDRESS
Here is a sample program that deletes a record from an
INDEXED file using the DELETE statement, followed by deletion of a record that
does not use the DELETE statement but writes the whole file (less the record to
be deleted) to a temporary file. The program asks for a six digit code that
identifies the record to be removed from the file. If you want to try this
program then you'll need to create a couple of test files: TESTDATA1.DAT and
TESTDATA1.DAT needs to be an indexed file. To create this
you'll need to compile and run the
Create INDEXED file program and
Read INDEXED file program (both in the Sample Code section).
TESTDATA2.TXT should be LINE SEQUENTIAL and of the form:
CODE--SOME ENTRY OF 43 CHARACTERS