MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL WRITE and END-WRITE

The WRITE statement outputs a record to a file that has been opened for output or input-output. WRITE is used for file output operations, writing data records to files defined in the FILE SECTION. END-WRITE is an explicit scope terminator that marks the end of the WRITE statement's scope. Understanding WRITE is essential for creating output files, generating reports, and writing data to persistent storage in COBOL programs.

What is WRITE?

WRITE outputs a record to a file. Key characteristics:

  • File output: Writes to files, not console (use DISPLAY for console)
  • Requires file definition: File must be defined in FILE-CONTROL and FILE SECTION
  • Requires file to be open: File must be opened before writing
  • Record-based: Writes one record at a time
  • File organization dependent: Behavior varies by file type (sequential, indexed, etc.)

Basic Syntax

cobol
1
2
3
4
5
WRITE record-name [FROM identifier] [INVALID KEY imperative-statement] [NOT INVALID KEY imperative-statement] [END-WRITE]

Writing to Sequential Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT OUTPUT-FILE ASSIGN TO 'OUTPUT.DAT' ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD OUTPUT-FILE LABEL RECORDS ARE STANDARD. 01 OUTPUT-RECORD PIC X(100). WORKING-STORAGE SECTION. 01 WS-DATA PIC X(100). PROCEDURE DIVISION. WRITE-SEQUENTIAL. OPEN OUTPUT OUTPUT-FILE *> Method 1: Move then write MOVE 'Record 1 Data' TO OUTPUT-RECORD WRITE OUTPUT-RECORD *> Method 2: Write with FROM clause MOVE 'Record 2 Data' TO WS-DATA WRITE OUTPUT-RECORD FROM WS-DATA *> Method 3: With END-WRITE MOVE 'Record 3 Data' TO OUTPUT-RECORD WRITE OUTPUT-RECORD END-WRITE CLOSE OUTPUT-FILE STOP RUN.

Writing to Indexed Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INDEXED-FILE ASSIGN TO 'INDEXED.DAT' ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUSTOMER-KEY. DATA DIVISION. FILE SECTION. FD INDEXED-FILE LABEL RECORDS ARE STANDARD. 01 INDEXED-RECORD. 05 CUSTOMER-KEY PIC 9(8). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-BALANCE PIC 9(9)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS PIC X(2). PROCEDURE DIVISION. WRITE-INDEXED. OPEN OUTPUT INDEXED-FILE *> Set key and data MOVE 12345678 TO CUSTOMER-KEY MOVE 'JOHN SMITH' TO CUSTOMER-NAME MOVE 1500.00 TO CUSTOMER-BALANCE *> Write with INVALID KEY handling WRITE INDEXED-RECORD INVALID KEY DISPLAY 'ERROR: Duplicate key' MOVE FILE-STATUS TO FILE-STATUS NOT INVALID KEY DISPLAY 'Record written successfully' END-WRITE *> Check file status IF FILE-STATUS NOT = '00' DISPLAY 'File status: ' FILE-STATUS END-IF CLOSE INDEXED-FILE STOP RUN.

Using FROM Clause

The FROM clause writes from a different field:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WORKING-STORAGE SECTION. 01 WS-CUSTOMER-DATA PIC X(100). PROCEDURE DIVISION. WRITE-WITH-FROM. *> Build data in working storage MOVE 'Customer Data Here' TO WS-CUSTOMER-DATA *> Write directly from working storage WRITE OUTPUT-RECORD FROM WS-CUSTOMER-DATA *> Equivalent to: *> MOVE WS-CUSTOMER-DATA TO OUTPUT-RECORD *> WRITE OUTPUT-RECORD STOP RUN.

File Status Checking

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
WORKING-STORAGE SECTION. 01 FILE-STATUS PIC X(2). PROCEDURE DIVISION. WRITE-WITH-STATUS-CHECK. OPEN OUTPUT OUTPUT-FILE MOVE 'Data' TO OUTPUT-RECORD WRITE OUTPUT-RECORD *> Always check file status after WRITE IF FILE-STATUS = '00' DISPLAY 'Write successful' ELSE IF FILE-STATUS = '23' DISPLAY 'ERROR: Duplicate key' ELSE IF FILE-STATUS = '30' DISPLAY 'ERROR: Permanent error' ELSE DISPLAY 'ERROR: File status ' FILE-STATUS END-IF END-IF END-IF CLOSE OUTPUT-FILE STOP RUN.

END-WRITE Scope Terminator

END-WRITE explicitly marks the end of WRITE statement scope:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
PROCEDURE DIVISION. WRITE-WITH-END-WRITE. *> END-WRITE provides explicit scope termination WRITE OUTPUT-RECORD INVALID KEY DISPLAY 'Error writing record' END-WRITE *> Code after END-WRITE executes regardless of INVALID KEY DISPLAY 'Write operation completed' STOP RUN.

Best Practices

  • Always check file status: Verify WRITE operations succeeded
  • Handle INVALID KEY: For indexed files, always handle duplicate keys
  • Use FROM when appropriate: Simplifies code when writing from working storage
  • Close files properly: Always close files after writing
  • Validate data: Ensure data is valid before writing

Test Your Knowledge

1. What must you do before using WRITE?

  • Define the file and open it
  • Just define the file
  • Just open the file
  • Nothing, WRITE works automatically

2. What does the FROM clause do?

  • Specifies the file name
  • Writes from a different field than the record area
  • Specifies the record format
  • Handles errors

3. What is END-WRITE?

  • A file closing statement
  • An explicit scope terminator for WRITE
  • An error handler
  • A file opening statement

4. What file status indicates a duplicate key error?

  • 00
  • 23
  • 30
  • 10

5. Can you use WRITE with sequential files?

  • No, only indexed files
  • Yes, WRITE works with sequential files
  • Only with relative files
  • Only with VSAM files

Related Concepts

Related Pages