Compaq COBOL AAQ2G1FTK User Manual

Page 229

Advertising
background image

Processing Files and Records

6.5 Updating Files

Statements ( 1 ) and ( 2 ) in the following example are logically equivalent:

FILE SECTION.
FD

STOCK-FILE.

01

STOCK-RECORD

PIC X(80).

WORKING-STORAGE SECTION.
01

STOCK-WORK

PIC X(80).

---------------(1)------------------

--------------(2)--------------

REWRITE STOCK-RECORD FROM STOCK-WORK.

MOVE STOCK-WORK TO STOCK-RECORD.

REWRITE STOCK-RECORD.

When you omit the FROM phrase, you process the records directly in the record
area or buffer (for example, STOCK-RECORD).

For a REWRITE statement on a sequential file, the record being rewritten must
be the same length as the record being replaced.

Example 6–36 reads a sequential file and rewrites as many records as the
operator wants.

Example 6–36 Rewriting a Sequential File

IDENTIFICATION DIVISION.
PROGRAM-ID. SEQ03.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

SELECT TRANS-FILE ASSIGN TO "TRANS".

DATA DIVISION.
FILE SECTION.
FD

TRANS-FILE.

01

TRANSACTION-RECORD

PIC X(25).

WORKING-STORAGE SECTION.
01

ANSWER

PIC X.

PROCEDURE DIVISION.
A000-BEGIN.

OPEN I-O TRANS-FILE.
PERFORM A100-READ-TRANS-FILE

UNTIL TRANSACTION-RECORD = "END".

CLOSE TRANS-FILE.
STOP RUN.

A100-READ-TRANS-FILE.

READ TRANS-FILE AT END

MOVE "END" TO TRANSACTION-RECORD.

IF TRANSACTION-RECORD NOT = "END"

PERFORM A300-GET-ANSWER UNTIL ANSWER = "Y" OR "N"

IF ANSWER = "Y" DISPLAY "Please enter new record content"

ACCEPT TRANSACTION-RECORD
REWRITE TRANSACTION-RECORD.

A300-GET-ANSWER.

DISPLAY "Do you want to replace this record? -- "

TRANSACTION-RECORD.

DISPLAY "Please answer Y or N".
ACCEPT ANSWER.

Extending a Sequential or Line Sequential File

To position a file to its current end, and to allow the program to write new records
beyond the last record in the file, use both:

The EXTEND phrase of the OPEN statement

The WRITE statement

Processing Files and Records 6–49

Advertising