SELECT and FD

A file in COBOL is declared twice: once in the environment division, where it gets a name in the program and a name on the disk, and once in the data division, where the bytes of one record are described. Every statement that uses the file works between those two declarations.

SELECT and the file control

The FILE-CONTROL paragraph of the environment division holds one SELECT per file. It names the file inside the program, the path or device it is assigned to, the organisation of the records and a field that receives the status of every operation:

IDENTIFICATION DIVISION. PROGRAM-ID. MKFILE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUST-F ASSIGN TO "customers.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-STATUS. DATA DIVISION. FILE SECTION. FD CUST-F. 01 CUST-REC. 05 CUST-ID PIC X(3). 05 CUST-NAME PIC X(12). 05 CUST-AMT PIC 9(5). WORKING-STORAGE SECTION. 01 WS-STATUS PIC XX. PROCEDURE DIVISION. OPEN OUTPUT CUST-F. DISPLAY "open status " WS-STATUS. MOVE "A10" TO CUST-ID. MOVE "Ada Lovelace" TO CUST-NAME. MOVE 1250 TO CUST-AMT. WRITE CUST-REC. DISPLAY "write status " WS-STATUS. MOVE "B20" TO CUST-ID. MOVE "Grace Hopper" TO CUST-NAME. MOVE 725 TO CUST-AMT. WRITE CUST-REC. DISPLAY "write status " WS-STATUS. CLOSE CUST-F. DISPLAY "close status " WS-STATUS. STOP RUN.

cobc -x -free -Wall -o mkfile mkfile.cob
./mkfile

open status 00
write status 00
write status 00
close status 00

The FD and its record

The FD names the same file and the entries under it describe one record. CUST-REC is 20 bytes: three for the identifier, twelve for the name and five for the amount. There is no header and no separator in the record itself; whatever separates two records is the organisation of the file.

The record is the unit of READ and WRITE, and the fields under the FD are the only storage the file's statements use. A program that moves a field into CUST-REC from working storage is doing the same thing as moving into any other group; the difference is that the next WRITE sends it to the disk.

OPEN, WRITE and CLOSE

OPEN OUTPUT creates the file or truncates it, and the status is 00 when it worked. WRITE adds the current record, and CLOSE commits the last buffer. Every one of the three printed 00 here, and a program that does not print the status has no way to know it was not 30 or 35.

cat -A customers.dat

A10Ada Lovelace01250$
B20Grace Hopper00725$

cat -A writes a $ at the end of each line, so the two records are 20 bytes and a newline each. The file is 42 bytes, which the next section explains.

Line sequential files

ORGANIZATION IS LINE SEQUENTIAL writes each record as text and follows it with a newline, and it drops the trailing spaces of the record. That makes the file readable by every tool on the machine and costs one byte per record:

ls -l customers.dat

-rw-r--r-- 1 agent agent 42 Nov 18 09:09 customers.dat

42 is two records of 20 bytes plus two newlines. A record whose last field is spaces loses them on the way out, and a READ into the same record pads them back, so a program sees its record as declared; another program reading the same file sees a shorter line. The lesson on line sequential files is about that difference and the tools that make it visible.

Sequential files

ORGANIZATION IS SEQUENTIAL writes the records one after another with nothing between them. The same two records are 40 bytes, and a reader has to know the record length to find the second one:

IDENTIFICATION DIVISION. PROGRAM-ID. SEQFILE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUST-F ASSIGN TO "customers.seq" ORGANIZATION IS SEQUENTIAL FILE STATUS IS WS-STATUS. DATA DIVISION. FILE SECTION. FD CUST-F. 01 CUST-REC PIC X(20). WORKING-STORAGE SECTION. 01 WS-STATUS PIC XX. PROCEDURE DIVISION. OPEN OUTPUT CUST-F. MOVE "A10Ada Lovelace01250" TO CUST-REC. WRITE CUST-REC. MOVE "B20Grace Hopper00725" TO CUST-REC. WRITE CUST-REC. CLOSE CUST-F. DISPLAY "status " WS-STATUS. STOP RUN.

cobc -x -free -Wall -o seqfile seqfile.cob
./seqfile
ls -l customers.seq
hexdump -C customers.seq

status 00
-rw-r--r-- 1 agent agent 40 Nov 18 09:09 customers.seq
00000000 41 31 30 41 64 61 20 4c 6f 76 65 6c 61 63 65 30 |A10Ada Lovelace0|
00000010 31 32 35 30 42 32 30 47 72 61 63 65 20 48 6f 70 |1250B20Grace Hop|
00000020 70 65 72 30 30 37 32 35 |per00725|

The bytes 42 32 30 in the middle are the B20 of the second record, with no newline in front of them. That is the format a mainframe writes and the one a copybook of a fixed record describes; the reader of such a file needs the record length, and the program that reads it declares the same 20 bytes in its FD.

The next lesson reads both kinds of file back, with OPEN INPUT, READ and AT END, and makes the point about the status field that every one of these statements carries.

Exercises

  1. Change the path in ASSIGN TO to "/tmp/customers.dat" and run the program. Which directory holds the file now?
  2. Remove the FILE STATUS clause and build. Does the program still compile, and what can it no longer do?
  3. Add a third record whose name is shorter than twelve bytes and compare the two files again. Which one grew by fewer bytes?
  4. Describe a record of 80 bytes with five fields and write one. What is the size of the line sequential file and of the sequential one?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Where is a file declared in a COBOL program?

    Answer

    In the FILE-CONTROL paragraph and again as an FD with its record

    The SELECT names the file and its organisation, and the FD describes the bytes of one record.

  2. What does ASSIGN TO name?

    Answer

    The path or device the file is read from and written to

    The assignment is the link between the program name and the file on the disk.

  3. What does FILE STATUS IS WS-STATUS receive?

    Answer

    A two-character status after every operation on the file

    The program printed 00 after the open, each write and the close.

  4. How many bytes does a line sequential file use for a 20-byte record?

    Answer

    Twenty-one: the record and a newline

    Two records made a file of 42 bytes, and cat -A showed one $ at the end of each line.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-OUT ASSIGN TO "q5.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-OUT. 01 R-OUT PIC X(3). WORKING-STORAGE SECTION. 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-OUT. MOVE "ABC" TO R-OUT. WRITE R-OUT. CLOSE F-OUT. DISPLAY W-S. STOP RUN.

    Answer

    00

    The operations succeed, so the status field holds the success code.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 S-F PIC XX VALUE "00". 01 R-F PIC X(4) VALUE "ABCD". 01 O-F PIC X(6) VALUE SPACES. PROCEDURE DIVISION. MOVE R-F TO O-F. DISPLAY "[" O-F "] " S-F. STOP RUN.

    Answer

    [ABCD ] 00

    The move pads the four bytes to six with spaces on the right.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC 9(3) VALUE 1250. 01 B-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. MOVE A-F TO B-F. DISPLAY B-F. STOP RUN.

    Answer

    250

    A three-digit field cannot hold 1250, and the move keeps the low-order digits.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 C-F PIC X(5) VALUE "A10 ". 01 D-F PIC X(5) VALUE "B20 ". PROCEDURE DIVISION. DISPLAY "[" C-F "]" WITH NO ADVANCING. DISPLAY "[" D-F "]". STOP RUN.

    Answer

    [A10 ][B20 ]

    The clause keeps the two displays on one line, and each field prints its five bytes.

  9. A program writes 20-byte records with ORGANIZATION SEQUENTIAL and another program reads the file with a 40-byte FD. What happens to the data?

    Answer

    Every two records are read as one, because the file has no separators

    A sequential file carries no record boundaries, so the reader with the wrong length regroups the bytes.

  10. Why does the program print the status after every operation?

    Answer

    The status is the only signal that a file operation failed

    Without reading the field a program cannot tell a successful write from one that returned 30.

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

Banner image