Line sequential files
A line sequential file is the one COBOL shares with the rest of the machine: one record per line, a newline after each and the trailing spaces of a record left out. The program sees its record as declared; a reader with cat sees a shorter line, and a line that is too long comes back as two records.
What a line sequential file holds
The record below is thirteen bytes: three for the identifier and ten for the name. Both names are shorter than ten, so the program's record ends in spaces, and the file does not:
IDENTIFICATION DIVISION.
PROGRAM-ID. LSEQ.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT NAME-F ASSIGN TO "names.txt"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD NAME-F.
01 NAME-REC.
05 NAME-ID PIC X(3).
05 NAME-V PIC X(10).
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
PROCEDURE DIVISION.
OPEN OUTPUT NAME-F.
MOVE "A10" TO NAME-ID.
MOVE "Ada" TO NAME-V.
WRITE NAME-REC.
MOVE "B20" TO NAME-ID.
MOVE "Grace" TO NAME-V.
WRITE NAME-REC.
CLOSE NAME-F.
OPEN INPUT NAME-F.
PERFORM UNTIL WS-STATUS = "10"
READ NAME-F
AT END CONTINUE
NOT AT END
DISPLAY NAME-ID " [" NAME-V "] len "
LENGTH OF NAME-V
END-READ
END-PERFORM.
CLOSE NAME-F.
STOP RUN.
cobc -x -free -Wall -o lseq lseq.cob
./lseq
cat -A names.txt
A10 [Ada ] len 10
B20 [Grace ] len 10
A10Ada$
B20Grace$
Reading it back
The program printed the name with its ten bytes and three spaces, and the file has seven characters on the first line and eight on the second. The write dropped the padding and the read put it back, so the program that wrote the file and the program that reads it both see the record as thirteen bytes.
That is the difference from the fixed file of SELECT and FD: there two records were forty bytes with nothing between them, here each record is its own line and the length of the line is whatever the record had after its last non-space character. A program that needs the exact thirteen bytes on the disk, spaces included, uses ORGANIZATION IS SEQUENTIAL instead.
A line longer than the record
The file below is written by printf rather than by the program, which is the normal case: a text file arrives from another tool. One line is longer than the thirteen-byte record, and one has trailing spaces:
printf 'A10Ada\nB20Grace Hopper\nC30 \n' > people.txt
cat -A people.txt
A10Ada$
B20Grace Hopper$
C30 $
A program with the same FD reads it, and the long line is not truncated but split:
IDENTIFICATION DIVISION.
PROGRAM-ID. READTEXT.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT NAME-F ASSIGN TO "people.txt"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD NAME-F.
01 NAME-REC.
05 NAME-ID PIC X(3).
05 NAME-V PIC X(10).
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
PROCEDURE DIVISION.
OPEN INPUT NAME-F.
IF WS-STATUS NOT = "00"
DISPLAY "cannot open people.txt, status " WS-STATUS
STOP RUN
END-IF.
DISPLAY "open " WS-STATUS.
PERFORM UNTIL WS-STATUS = "10"
READ NAME-F
AT END CONTINUE
NOT AT END
DISPLAY "[" NAME-REC "]"
END-READ
END-PERFORM.
CLOSE NAME-F.
STOP RUN.
cobc -x -free -Wall -o readtext readtext.cob
./readtext
open 00
[A10Ada ]
[B20Grace Hopp]
[er ]
[C30 ]
The fifteen-character line became two records: the first thirteen bytes and the remaining two, padded. A program that reads a file it did not write sees the tail as a record of its own, which is why the FD of an incoming text file is as wide as the longest line the sender can produce, or wider.
The third line had three trailing spaces and the read returned a record padded to thirteen, so the spaces a reader of the file sees are part of the record. A line with none is the case of the first file and the padding is invisible in the file.
The file as text
The point of the organisation is that every tool on the machine can read the file: cat, grep, awk, a spreadsheet. A program that must hand its output to one of those writes line sequential; a program that must read what a mainframe wrote uses the fixed organisation of the previous lesson.
With no people.txt in the directory the second program prints cannot open people.txt, status 35 and stops: 35 is the status of a file that is not there, and the lesson on FILE STATUS is the table of the codes a program meets.
Exercises
- Write a record whose name fills all ten bytes and one whose name is one letter. Which lines of the file have trailing spaces left?
- Change the FD to PIC X(20) and read people.txt again. How many records does the long line make now?
- Add a line with a tab character and print the record in brackets. What does the tab look like?
- Write the same two records to a line sequential and to a sequential file and compare the two sizes with ls -l.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What separates two records in a line sequential file?
Answer
A newline after each record
cat -A showed one $ at the end of each record and no separator in the bytes.
-
What happens to the trailing spaces of a record when it is written?
Answer
They are dropped, and the read pads the record back to its length
The file holds A10Ada while the program read back Ada and seven spaces.
-
What happens to a line longer than the record?
Answer
The rest of the line becomes the next record, padded
The fifteen-character line arrived as two records, the second holding the remaining bytes.
-
Why does a program write line sequential output?
Answer
Other tools can read it as text, one record per line
The format is the one cat, grep and a spreadsheet already understand.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 R-F. 05 R-ID PIC X(3). 05 R-N PIC X(4). PROCEDURE DIVISION. MOVE "A10" TO R-ID. MOVE "Ada" TO R-N. DISPLAY "[" R-F "]". STOP RUN.Answer
[A10Ada ]
The group is seven bytes and the short name is padded to four.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC X(10) VALUE "Grace". 01 B-F PIC X(10) VALUE SPACES. PROCEDURE DIVISION. MOVE A-F TO B-F. DISPLAY "[" B-F "]". STOP RUN.Answer
[Grace ]
The name is five bytes in a ten-byte field, so five spaces follow it.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 L-F PIC X(10) VALUE "Ada". 01 N-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. INSPECT L-F TALLYING N-F FOR CHARACTERS BEFORE INITIAL SPACE. DISPLAY N-F. STOP RUN.Answer
03
The tally counts the characters before the first space, which are the three letters.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 R-F. 05 R-ID PIC X(3). 05 R-N PIC X(10). PROCEDURE DIVISION. DISPLAY LENGTH OF R-F. STOP RUN.Answer
13
The group is the sum of its fields, three and ten.
-
A program reads a text file with an FD of ten bytes and the lines are up to forty. What does it see?
Answer
Every long line as several records, so the data arrives in pieces
The long line of the second program came back as two records, and a forty-byte line would come back as four.
-
What is status 35?
Answer
The file is not there
The program printed cannot open people.txt, status 35 when the file was absent.
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