Sequential Files

LINE SEQUENTIAL is the natural choice for text files on PCs (standardized in COBOL 2023; long supported by GnuCOBOL and Micro Focus).

21.1 File concepts

SELECT... ASSIGN Connects an internal file name to an external file (path on Linux/Windows, DD name on z/OS)

SEQUENTIAL (record-oriented, fixed/variable), LINE SEQUENTIAL (text lines terminated by newline), RELATIVE, INDEXED SEQUENTIAL, RANDOM, DYNAMIC

File description in the FILE SECTION, followed by record layouts

A 2-character field updated after every I/O operation (see Appendix 45.4)

LINE SEQUENTIAL is the natural choice for text files on PCs (standardized in COBOL 2023; long supported by GnuCOBOL and Micro Focus). On z/OS, SEQUENTIAL files are QSAM datasets with fixed (RECFM=FB) or variable (RECFM=VB) records; IBM also supports line-sequential files in z/OS UNIX.

21.2 OPEN modes and verbs

OPEN INPUT f Read from the beginning OPEN OUTPUT f Create (or replace) and write OPEN EXTEND f Append to the end

Read and REWRITE in place (not for line sequential in most compilers)

READ f [INTO ws] AT END... NOT AT END... END-READ Read next record WRITE rec [FROM ws]

Write a record (note: you WRITE a *record*, READ a *file*) Replace the last record read (I-O mode) Close the file

21.3 Complete example: customer listing with totals

Input file customers.txt (ID 6, name 25, balance as sign + 9 digits with implied decimals):

This "priming read" pattern — read once before the loop, then read again at the end of each iteration — is the standard COBOL sequential processing idiom.

000101John Smith               +000150075
000102Mary Johnson             -000002550
000103Ahmed Khan               +001200000
IDENTIFICATION DIVISION.
       PROGRAM-ID. CUSTLIST.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT CUST-FILE ASSIGN TO "customers.txt"
               ORGANIZATION IS LINE SEQUENTIAL
               FILE STATUS IS WS-CUST-FS.
           SELECT RPT-FILE ASSIGN TO "custlist.txt"
               ORGANIZATION IS LINE SEQUENTIAL
               FILE STATUS IS WS-RPT-FS.
       DATA DIVISION.
       FILE SECTION.
       FD  CUST-FILE.
       01  CUST-REC.
           05  CUST-ID           PIC 9(6).
           05  CUST-NAME         PIC X(25).
           05  CUST-BALANCE      PIC S9(7)V99
                                 SIGN IS LEADING SEPARATE.
       FD  RPT-FILE.
       01  RPT-LINE              PIC X(80).
       WORKING-STORAGE SECTION.
       01  WS-CUST-FS            PIC XX.
       01  WS-RPT-FS             PIC XX.
       01  WS-EOF-SW             PIC X VALUE "N".
           88  END-OF-CUST       VALUE "Y".
       01  WS-COUNT              PIC 9(5)     VALUE 0.
       01  WS-TOTAL              PIC S9(9)V99 VALUE 0.
       01  HEADING-LINE          PIC X(46) VALUE
           "ID      NAME                           BALANCE".
       01  DE
…

21.4 IBM z/OS notes

ASSIGN TO CUSTIN names the DD statement in the JCL; the dataset name lives in JCL, not in the program.

BLOCK CONTAINS 0 RECORDS lets the system choose an optimal block size. RECORDING MODE IS V with RECORD VARYING IN SIZE FROM 20 TO 200 CHARACTERS DEPENDING ON WS-REC-LEN for variable-length records.

Printer files often use WRITE ... AFTER ADVANCING 1 LINE / AFTER ADVANCING PAGE (with carriage-control characters, RECFM=FBA).

SELECT CUST-FILE ASSIGN TO CUSTIN
               FILE STATUS IS WS-CUST-FS.
      * ...
       FD  CUST-FILE
           RECORDING MODE IS F
           BLOCK CONTAINS 0 RECORDS.
       01  CUST-REC              PIC X(80).
//CUSTIN   DD DSN=PROD.CUSTOMER.MASTER,DISP=SHR
//RPTOUT   DD SYSOUT=*