Printing from Cobol Programs


Please carefully read pages 707-708 in Appendix B of your text book. This will provide information about generating printed output from your Cobol programs.

In a mainframe environment it would be fairly easy to provide specific directions about printed output. However, we are for the most part using home PCs and, therefore, do not enjoy the luxury of a standard operating environment as we would on a mainframe.

Attached Printers

As indicated in your text, personal computers have output ports to which peripheral devices, such as printers, can be attached. Commonly, the LPT1 port is used for your printer. Using "ASSIGN TO PRINTER" in the SELECT statment may, depending on the configuration of your PC, direct output to your printer.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT OUT-FILE
               ASSIGN TO PRINTER.

Your textbook also suggests "ASSIGN TO 'LPT1:' as equivalent.

Network Printers

Keeping in mind that the Micro Focus Cobol software we are using is actually a DOS/Windows 3.1 application (rather than Windows 95/98), it should not come as a surprise that the software does not support the newer Windows print spooling capability. If your printer is on a network with your PC rather than being directly attached to an output port on your PC the "ASSIGN TO PRINTER" method will not work unless you can find out how to associate a port name on your PC with a network printer.

Your text book suggests the use of the NET USE command which you enter at the DOS prompt. To get the DOS prompt click on your Start button, point to Programs and then to MS-DOS Prompt. You will need to enter a NET USE command something like: NET USE LPT1: \\computer\printer

You will need to replace \\computer\printer with the path to the specific network printer you want to use. To find that path try clicking on your Start button and pointing to Settings and then Printers. When the Printers window opens, right-click on your printer and select Properties from the pop-up menu. In the Properties window click on the Details tab and use the path found in the box labeled "Print to the following port". Click Cancel to close the Properties window.

Output to Disk File

An alternative to sending your program's output directly to a printer is sending it to a disk file which can then be printed later using any software application that is capable of printing text files.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT OUT-FILE
               ASSIGN TO 'A:\REPORT.TXT'
               ORGANIZATION IS LINE SEQUENTIAL.

Creating printed output in this way is therefore a two-step process: (1) you run your Cobol program to create an output file on disk, and (2) you open and print that disk file from some other application.

The application mentioned in your text book is Notepad. Notepad is a standard utility provided with Windows. Most personal computers are configured with Notepad as the standard text editing application. To open Notepad point to the Start button, then to Programs, then to Accessories, then to Notepad. Be aware, however, that Notepad does have some limitations and there are other alternatives.

One of the problems with writing your output to a text file instead of directly to the printer is that Notepad and other text editors do not always handle the control characters inserted into the file when you use Cobol statements such as:

            WRITE PRINT-LINE AFTER ADVANCING PAGE.

The examples below show two paragraphs which should create the same output. But, the two output files look quite different when opened with Notepad.

HELLO6.CBL HELLO6X.CBL
PRINT-FOOTERS.
    MOVE SPACES TO OUT-RECORD.
    WRITE OUT-RECORD.
    MOVE THE-COUNT TO FOOTER-COUNT.
    MOVE FOOTER-LINE TO OUT-RECORD.
    WRITE OUT-RECORD.
PRINT-FOOTERS.
    MOVE THE-COUNT TO FOOTER-COUNT.
    MOVE FOOTER-LINE TO OUT-RECORD.
    WRITE OUT-RECORD AFTER 2.
Output from Hello-6 Output from Hello-6x

The best method for producing printed output will likely vary from one student to another. The instructor is aware that this is a limitation of our software environment which will require some degree of flexibility.


Cobol Home