[ Back to the overview Matrix ]

Test case : Type "file" using Cobol

Lines used: 49
Here's a COBOL85 (with one extension, since ANSI '85 COBOL doesn't have a way to read parameters from the command line) implementation of the "type a file" problem for APLC.
       identification division.
       program-id. TypeFile.

      * TypeFile: COBOL85 program to read a file and display it on the
      * screen.  This is a minimal implementation which reads the file
      * 1 byte at a time, displaying the character as it goes.

       environment division.
       input-output section.
       file-control.

           select infile assign infile-name
           organization is sequential
           file status infile-status.

       data division.
       file section.

       fd infile.
       01 infile-rec        pic x.

       working-storage section.
       77 infile-name       pic x(80).
       77 infile-status     pic xx value "00".
       77 status-display    pic 99 display.

       01 infile-eof-flag   pic x value "n".
          88 infile-eof     value "y".

       77 infile-char       pic x.


       procedure division.

       main-paragraph.

      * Get the filename from the command line
      * This can't be done in ANSI-85 COBOL; use Micro Focus extension.
           accept infile-name from command-line

      * Open the file
           open input infile

      * Read the first character (status "00" means file opened OK)
           if infile-status = "00" then
               perform read-character
           end-if

      * Display character and read next one until EOF or error
           perform until infile-status not = "00"
               display infile-char with no advancing
               perform read-character
           end-perform

      * Log any I/O error
           if not infile-eof
               move infile-status to status-display
               display " "
               display "File I/O error code: " with no advancing
               display status-display
           end-if
           stop run.

       read-character.
           read infile record into infile-char
               at end set infile-eof to true.
Contributed by Michael Wojcik, Michael.Wojcik at microfocus.com