OPEN and CLOSE modes
A file is opened in one of four modes, and the mode decides which verbs the program may use until the close. A verb outside the mode does not crash the program: it leaves a status and the statement does nothing, which is why the status field belongs next to every open, read and write.
OPEN OUTPUT truncates
OPEN OUTPUT creates the file if it is not there and throws away what it holds if it is. The program below writes two records, closes, opens the same file for EXTEND, writes a third, closes and then reads all three:
IDENTIFICATION DIVISION.
PROGRAM-ID. MODES.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT LOG-F ASSIGN TO "log.txt"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD LOG-F.
01 LOG-REC PIC X(10).
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
PROCEDURE DIVISION.
OPEN OUTPUT LOG-F.
DISPLAY "open output " WS-STATUS.
MOVE "first" TO LOG-REC.
WRITE LOG-REC.
MOVE "second" TO LOG-REC.
WRITE LOG-REC.
CLOSE LOG-F.
OPEN EXTEND LOG-F.
DISPLAY "open extend " WS-STATUS.
MOVE "third" TO LOG-REC.
WRITE LOG-REC.
CLOSE LOG-F.
OPEN INPUT LOG-F.
DISPLAY "open input " WS-STATUS.
READ LOG-F
AT END CONTINUE
NOT AT END DISPLAY "read [" LOG-REC "]"
END-READ.
WRITE LOG-REC.
DISPLAY "write on input " WS-STATUS.
CLOSE LOG-F.
STOP RUN.
cobc -x -free -Wall -o modes modes.cob
./modes
cat log.txt
open output 00
open extend 00
open input 00
read [first ]
write on input 48
first
second
third
OPEN EXTEND appends
EXTEND opens the file for writing at its end, so the three records are still there after the second close. It is the mode a log uses: a program that adds a line must not rewrite the file, and OUTPUT would.
A file opened with OUTPUT is empty from the moment of the open, even if the program writes nothing and closes at once. A program that wants to keep an existing file has exactly one choice among the four modes, and it is this one.
OPEN INPUT reads
INPUT allows READ and nothing that changes the file. The program tried to WRITE in that mode and the statement left 48 in the status: "not opened for output", and the file kept its three lines.
A status other than 00 after a write means the record did not go to the disk. The program continued and printed the status, which is the only reason the failed write is visible at all; a program that ignores the field ends as if every record had been written.
OPEN I-O and its limits
I-O is the mode that allows both reading and rewriting. For an indexed file it is the normal mode of an update program; for a line sequential file this compiler accepts the open and refuses the write:
IDENTIFICATION DIVISION.
PROGRAM-ID. IOMODE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT LOG-F ASSIGN TO "io.txt"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD LOG-F.
01 LOG-REC PIC X(10).
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
PROCEDURE DIVISION.
OPEN OUTPUT LOG-F.
MOVE "one" TO LOG-REC.
WRITE LOG-REC.
CLOSE LOG-F.
OPEN I-O LOG-F.
DISPLAY "open i-o " WS-STATUS.
READ LOG-F
AT END DISPLAY "i-o read at end"
NOT AT END DISPLAY "i-o read [" LOG-REC "]"
END-READ.
WRITE LOG-REC.
DISPLAY "write in i-o " WS-STATUS.
CLOSE LOG-F.
OPEN OUTPUT LOG-F.
DISPLAY "open output again " WS-STATUS.
READ LOG-F
AT END DISPLAY "output read at end"
END-READ.
DISPLAY "read on output " WS-STATUS.
CLOSE LOG-F.
STOP RUN.
cobc -x -free -Wall -o iomode io.cob
./iomode
open i-o 00
i-o read [one ]
write in i-o 48
open output again 00
read on output 47
The codes of a wrong operation
Two codes came out of the two programs, and both mean "the verb is not available in this mode": 48 for a write to a file opened for input or I-O on this organisation, and 47 for a read from a file opened for output. Neither closed the file and neither stopped the run.
The last open in the program is the other lesson: OPEN OUTPUT truncated the file that had held the record one, and the read after it found nothing. A program that opens the same file twice in one run has to close it in between and to know which mode it wants; the next lesson is the table of the statuses, and the one after that puts REWRITE and DELETE in the mode that allows them.
Exercises
- Open the file with OUTPUT twice in a row without writing between them. How many lines does the file have at the end?
- Try DELETE on the file opened for input and print the status. Which code comes back?
- Open a file that does not exist with INPUT and print the status. Which code comes back, and what does EXTEND do with the same name?
- Add EXTEND to a file after an INPUT open without closing it. What does the second open return?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does OPEN OUTPUT do to a file that already has records?
Answer
It truncates the file, so the records are gone
The three records of the program were written after an OUTPUT open, and the file held only the new ones at the end of that step.
-
Which mode adds records to the end of a file?
Answer
EXTEND
The third record was written in EXTEND and the first two survived.
-
What does a WRITE on a file opened for INPUT return?
Answer
Status 48, and the file is unchanged
The program printed 48 after the write and cat showed the three records untouched.
-
What does a READ on a file opened for OUTPUT return?
Answer
Status 47
The open output reset the file and the read came back with 47 and no AT END branch.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-O ASSIGN TO "q5.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-O. 01 R-O PIC X(4). WORKING-STORAGE SECTION. 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-O. MOVE "AA" TO R-O. WRITE R-O. CLOSE F-O. OPEN EXTEND F-O. MOVE "BB" TO R-O. WRITE R-O. CLOSE F-O. OPEN INPUT F-O. READ F-O AT END CONTINUE NOT AT END DISPLAY R-O END-READ. CLOSE F-O. STOP RUN.Answer
AA
The first read returns the record written before the EXTEND, which is AA.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-O ASSIGN TO "q6.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-O. 01 R-O PIC X(4). WORKING-STORAGE SECTION. 01 W-S PIC XX. PROCEDURE DIVISION. OPEN INPUT F-O. DISPLAY W-S. STOP RUN.Answer
35
The file does not exist, and an input open of a missing file returns 35.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 S-F PIC XX VALUE "48". PROCEDURE DIVISION. IF S-F = "00" DISPLAY "written" ELSE DISPLAY "not written" END-IF. STOP RUN.Answer
not written
The status is not the success code, so the else branch reports the failure.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC X(5) VALUE "first". 01 B-F PIC X(5) VALUE "secnd". PROCEDURE DIVISION. DISPLAY "[" A-F "] [" B-F "]". STOP RUN.Answer
[first] [secnd]
Both literals fill their five-byte fields exactly, so the brackets sit next to the text.
-
A program opens a report file with OUTPUT at the start of every run and the file is empty after a crash. What is the cause?
Answer
OUTPUT truncated the file at the open and the records were not written yet
The truncation happens at the open, so an interrupted run leaves nothing behind.
-
Which mode would an update program use for a file it reads and rewrites?
Answer
I-O, on a file organisation that supports rewriting
I-O allows READ and REWRITE in one pass, which the indexed-file lessons use.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev