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)------------------
REWRITE STOCK-RECORD FROM STOCK-WORK.
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
WORKING-STORAGE SECTION.
01 ANSWER
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
--------------(2)--------------
MOVE STOCK-WORK TO STOCK-RECORD.
REWRITE STOCK-RECORD.
PIC X(25).
PIC X.
Processing Files and Records 6–49
6.5 Updating Files
Need help?
Do you have a question about the COBOL AAQ2G1FTK and is the answer not in the manual?