Menu
CoddyTech

OPEN EXTEND

Part of the Files and Reports section of Coddy's COBOL Programming journey. Lesson 9 of 22.

OPEN OUTPUT starts a data set from nothing: the records already in it are lost. OPEN EXTEND keeps them and writes after the last one:

OPEN EXTEND LOG-FILE.
MOVE "Run ended" TO LOG-REC.
WRITE LOG-REC.
CLOSE LOG-FILE.

Logs and audit trails grow this way: each run adds its records at the end of the same data set.

OPEN EXTEND needs the data set to exist, or the status is 35. A SELECT OPTIONAL lets it create the data set instead, with status 05:

SELECT OPTIONAL LOG-FILE ASSIGN TO LOGFILE
    ORGANIZATION IS LINE SEQUENTIAL.
challenge icon

Challenge

Easy

Add Day 3 closed at the end of LOGFILE: open LOG-FILE with EXTEND, move the text to LOG-REC, write it and close the file. The program then reads the log back and prints all three days.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. EXTEND.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT LOG-FILE ASSIGN TO LOGFILE
               ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       FILE SECTION.
       FD LOG-FILE.
       01 LOG-REC PIC X(20).
       WORKING-STORAGE SECTION.
       01 WS-EOF PIC X VALUE "N".
           88 END-OF-FILE VALUE "Y".
       PROCEDURE DIVISION.
           *> Add Day 3 closed to LOGFILE here
           OPEN INPUT LOG-FILE.
           PERFORM UNTIL END-OF-FILE
               READ LOG-FILE
                   AT END SET END-OF-FILE TO TRUE
                   NOT AT END DISPLAY LOG-REC
               END-READ
           END-PERFORM.
           CLOSE LOG-FILE.
           STOP RUN.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Files and Reports