MainframeMaster

COBOL File Operations

File operations in COBOL are how your program connects to datasets, reads records, writes new data, updates or deletes existing records, and then releases the file. This page covers the core verbs—OPEN, READ, WRITE, REWRITE, DELETE, and CLOSE—and how to use them safely with file status checking.

Explain Like I'm Five: What Are File Operations?

Imagine a filing cabinet. Before you can look at or change any papers, you have to open the drawer (OPEN). To look at one paper at a time in order, you take the next one out (READ). To add a new paper at the back, you open the last drawer and put it in (WRITE with EXTEND). To change a paper that’s already in the cabinet, you find it, take it out, change it, and put it back (READ then REWRITE). When you’re done, you close the drawer (CLOSE). File operations in COBOL are the same idea: open the file, read or write records, then close it.

Declaring a File: SELECT and FD

Before you can use any file operation, the file must be declared in the Environment Division (SELECT ... ASSIGN) and the Data Division (FD and record layout). The SELECT links the logical file name used in the program to the actual dataset (e.g. via a JCL DD name). The FD describes the record layout and optional file attributes.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO CUSTFILE ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE RECORDING MODE IS F BLOCK CONTAINS 0 RECORDS. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(6). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC 9(9)V99. WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC X(2).

ASSIGN TO CUSTFILE means the runtime will use the DD name CUSTFILE (or the assigned path) to find the dataset. ORGANIZATION IS SEQUENTIAL means records are stored one after another; you read or write in order. FILE STATUS IS WS-FILE-STATUS is a two-byte field that gets set after every file operation: "00" means success, other values mean an error or end-of-file. You must check this field after each file I/O.

The OPEN Statement

OPEN connects your program to the file. Until you OPEN a file, you cannot READ, WRITE, REWRITE, or DELETE. The mode you choose determines what operations are allowed and whether the file must already exist.

OPEN modes and when to use them
ModePurposeTypical use
INPUTRead existing records only.Report programs, batch reads, validation passes. File must already exist.
OUTPUTCreate a new file and write records.Creating new files. Any existing file with the same name is typically deleted first.
I-ORead and update existing records.Update-in-place, REWRITE, DELETE. File must exist. Used with indexed or relative files.
EXTENDAdd records to the end of an existing file.Appending logs, adding new transactions without rewriting the whole file.

INPUT and OUTPUT are the most common for batch jobs: one program writes a file with OPEN OUTPUT, another reads it with OPEN INPUT. I-O is for update jobs where you read a record, change it, and REWRITE it. EXTEND is for appending without rewriting the entire file.

cobol
1
2
3
4
5
6
7
PROCEDURE DIVISION. OPEN-FILE. OPEN INPUT CUSTOMER-FILE IF WS-FILE-STATUS NOT = '00' DISPLAY 'OPEN failed: ' WS-FILE-STATUS STOP RUN END-IF.

You can open multiple files in one OPEN statement (e.g. OPEN INPUT FILE-A, OUTPUT FILE-B). Always check the file status after OPEN; if it is not "00", do not perform READ or WRITE on that file.

Reading Records: READ

READ retrieves the next record from a file opened as INPUT or I-O. For sequential files, "next" means the next physical record. Each successful READ advances the file position. You must handle end-of-file (no more records) using AT END or the file status.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
READ-RECORDS. PERFORM UNTIL WS-FILE-STATUS = '10' READ CUSTOMER-FILE AT END EXIT PERFORM NOT AT END ADD 1 TO RECORD-COUNT PERFORM PROCESS-CUSTOMER END-READ IF WS-FILE-STATUS NOT = '00' AND WS-FILE-STATUS NOT = '10' DISPLAY 'READ error: ' WS-FILE-STATUS EXIT PERFORM END-IF END-PERFORM.

AT END is taken when there are no more records; the file status is often set to "10". NOT AT END is taken when a record was read; the record is in the FD record area (here, CUSTOMER-RECORD). You can also use READ ... INTO working-storage-item to move the record into a working-storage copy in one step. For indexed files you can READ file RECORD KEY IS key-name to read by key, or sequential READ for next in key order.

Writing Records: WRITE

WRITE sends one record to a file that was opened as OUTPUT or EXTEND. The record must be in the FD record area (or you use FROM). For OUTPUT, each WRITE adds a new record; for EXTEND, each WRITE appends to the end. You cannot WRITE to a file opened only as INPUT.

cobol
1
2
3
4
5
6
7
8
WRITE-RECORD. MOVE 123456 TO CUST-ID MOVE 'ACME CORP' TO CUST-NAME MOVE 5000.00 TO CUST-BALANCE WRITE CUSTOMER-RECORD IF WS-FILE-STATUS NOT = '00' DISPLAY 'WRITE error: ' WS-FILE-STATUS END-IF.

For line-sequential or certain other file types, WRITE may add line endings or padding. For fixed-length records (RECORDING MODE F), the record length is determined by the FD record definition.

Updating and Deleting: REWRITE and DELETE

REWRITE replaces the last record that was read. The file must be opened as I-O. You read a record, change the FD record area, then REWRITE. You cannot REWRITE a record you have not just READ (for that file). DELETE removes the last record read from a file opened I-O; it is supported for indexed and relative files, not for purely sequential files.

cobol
1
2
3
4
5
6
7
8
9
UPDATE-RECORD. READ CUSTOMER-FILE IF WS-FILE-STATUS = '00' ADD 100.00 TO CUST-BALANCE REWRITE CUSTOMER-RECORD IF WS-FILE-STATUS NOT = '00' DISPLAY 'REWRITE error: ' WS-FILE-STATUS END-IF END-IF.

For indexed files, the key in the record must not be changed between READ and REWRITE. Changing the key would require DELETE and then WRITE of a new record.

Closing the File: CLOSE

CLOSE breaks the connection between the program and the file. Buffered data is written out, and resources are released. You should CLOSE every file you opened before the program ends. CLOSE is not optional for correct behavior and data integrity.

cobol
1
2
3
4
5
CLOSE-FILE. CLOSE CUSTOMER-FILE IF WS-FILE-STATUS NOT = '00' DISPLAY 'CLOSE error: ' WS-FILE-STATUS END-IF.

You can close multiple files in one statement: CLOSE CUSTOMER-FILE, REPORT-FILE. After CLOSE, the file cannot be used again until you OPEN it again.

File Status Codes

The file status (two characters) is set after every OPEN, READ, WRITE, REWRITE, DELETE, and CLOSE. The first character indicates category; the second often gives detail. Always check the status and branch accordingly.

Common file status codes
CodeMeaningWhat to do
00Success.Continue processing.
35File not found.Check DD name or path; create file first for OUTPUT/EXTEND if required.
37Open mode not allowed (e.g. OPEN OUTPUT on file that cannot be created).Check permissions and OPEN mode.
10End of file (no more records).Normal for sequential READ; exit read loop.
23Record not found (indexed/relative).Invalid key or missing record; check key or handle as error.

Status "00" means the operation succeeded. "10" is normal for end-of-file on a READ. Codes in the 30s are usually permanent errors (e.g. file not found, wrong mode). Implementation-specific codes exist; consult your compiler or runtime documentation for the full list.

Step-by-Step: Reading a Sequential File

  1. Declare the file in FILE-CONTROL (SELECT ... ASSIGN ... FILE STATUS) and in the FILE SECTION (FD and 01 record).
  2. OPEN INPUT the file. Check file status; if not "00", handle the error and do not read.
  3. In a loop: READ the file. If AT END or status "10", leave the loop. If status "00", process the record (it is in the FD record area).
  4. If you get any other status, handle the error (message, retry, or stop).
  5. When done, CLOSE the file and check the file status.

Step-by-Step: Creating a New File and Writing Records

  1. Declare the file with SELECT and FD as above.
  2. OPEN OUTPUT the file. This typically creates or replaces the dataset. Check file status.
  3. For each record to write: move data into the FD record area, then WRITE the record. Check file status after each WRITE.
  4. CLOSE the file. Check file status. After CLOSE, the data is persisted and the file is no longer in use by the program.

Sequential vs Indexed File Operations

With sequential organization, you only read or write in order. READ always gives the next record; there is no “read by key.” WRITE adds to the end. REWRITE and DELETE are not used for standard sequential files.

With indexed (or relative) organization, you can read the next record in key order or read a specific record by key. After a READ, you can REWRITE the same record or DELETE it. START can position the file at a key before a series of sequential READs. The exact verbs and syntax depend on the compiler and platform (e.g. VSAM on z/OS).

READ INTO and WRITE FROM

READ ... INTO moves the record from the file buffer into a working-storage item in one step. WRITE ... FROM does the opposite: it moves from a working-storage item into the file record area and then writes. This avoids an extra MOVE when you want the record in a different layout or when you are building the record in working storage. The INTO or FROM item must be at least as long as the record (or the compiler may truncate/pad per rules).

cobol
1
2
3
4
5
6
7
8
9
10
11
WORKING-STORAGE SECTION. 01 WS-CUSTOMER-COPY. 05 WS-CUST-ID PIC 9(6). 05 WS-CUST-NAME PIC X(30). 05 WS-CUST-BALANCE PIC 9(9)V99. READ CUSTOMER-FILE INTO WS-CUSTOMER-COPY AT END EXIT PERFORM NOT AT END PERFORM PROCESS-USING-WS-COPY END-READ.

Best Practices

Test Your Knowledge

Test Your Knowledge

1. Which OPEN mode would you use to only read records from an existing file?

  • OUTPUT
  • INPUT
  • EXTEND
  • I-O

2. What should you do after every OPEN, READ, WRITE, or CLOSE to handle failures?

  • Ignore errors
  • Check file status
  • Use DISPLAY only
  • Restart the program

3. EXTEND is used to:

  • Create a new file
  • Read the first record
  • Add records at the end of an existing file
  • Delete records