Cobol Printing Basics

You already have learned most of the steps for printing, because in COBOL you send data to the printer by writing data to a file. In COBOL, the printer is defined as a file, and it is opened, closed, and written to as though it were a file.

Like any file, the printer file requires both a logical and a physical definition. compare the differences between a SELECT statement for a file and the SELECT statement for a print file.

The difference between the listings is the ASSIGN clause at line 00800. A normal disk file is assigned to a filename on the disk. A printer file is assigned to the COBOL reserved word PRINTER.

At line 000900, the ORGANIZATION for the printer file is changed to LINE SEQUENTIAL.

A line sequential file is similar to a sequential file, but it usually has carriage return and line feed characters added to the end of each record. These are added automatically every time a record is written. This format is suitable for printing or displaying. The format also is suitable for a file that is to be edited using a text editor. In fact, the source code files that you are editing whenever you write a program are line sequential files. In this case, the printer file is organized as a line sequential file because a carriage return and line feed are "added" at the end of each line and are sent to the printer to cause the print head to return to the left margin (carriage return) and drop down one line (line feed).

Controlling the printer with WRITE
Code Effect
WRITE PRINTER-RECORD BEFORE ADVANCING 1. Sends the characters in PRINTER-RECORD to the printer, then advances the printer one line
WRITE PRINTER-RECORD AFTER ADVANCING 1. Advances the printer one line, then sends the characters in PRINTER-RECORD to the printer
WRITE PRINTER-RECORD BEFORE ADVANCING 5. Sends the characters in PRINTER-RECORD to the printer, then advances the printer five lines
WRITE PRINTER-RECORD AFTER ADVANCING 3. Advances the printer three lines, then sends the characters in PRINTER-RECORD to the printer
WRITE PRINTER-RECORD BEFORE ADVANCING PAGE. Sends the characters in PRINTER-RECORD to the printer, then advances the printer to the next page
WRITE PRINTER-RECORD AFTER ADVANCING PAGE. Advances the printer to the next page, then sends the characters in PRINTER-RECORD to the printer
A SELECT statement for a file
000400 ENVIRONMENT DIVISION.
000500 INPUT-OUTPUT SECTION.
000600 FILE-CONTROL.
000700     SELECT PHONE-FILE
000800         ASSIGN TO "phone.dat"
000900         ORGANIZATION IS SEQUENTIAL.
001000
001100 DATA DIVISION.
A SELECT statement for a print file
000400 ENVIRONMENT DIVISION.
000500 INPUT-OUTPUT SECTION.
000600 FILE-CONTROL.
000700     SELECT PHONE-FILE
000800         ASSIGN TO PRINTER
000900         ORGANIZATION IS LINE SEQUENTIAL.
001000
001100 DATA DIVISION.
The file definition for a printer file
003000 FD  PRINTER-FILE
003100     LABEL RECORDS ARE OMITTED.
003200 01  PRINTER-RECORD           PIC X(80).
003300

Most earlier COBOL programs use AFTER ADVANCING for carriage control. In fact, if you omit carriage control information when writing to a print file, each WRITE is treated as if you had included AFTER ADVANCING 1.

Most of the examples in this book use BEFORE ADVANCING. BEFORE ADVANCING has advantages for modern printers, particularly laser printers that expect to receive a form feed as the last command. This form feed causes the laser printer to print all of the last data received and eject the last page of the report.

It is a good practice to use either BEFORE or AFTER consistently in all of your code. You will see in more complex printing programs that it's easier to tell which line you are on when BEFORE or AFTER is used consistently in a program.