To go way off topic. COBOL.

Inspired by a recent StackOverflow question about reading a file line by line in various languages. I quickly (evidently not quickly enough) dug up some 3rd year Uni labs on COBOL and tried to clean it up into a basic file reader. The post was closed up and made into a community wiki, while I was putting the comments on my COBOL application.

So I’ll be sharing it here. But first some background.

Back then we were writing labs and assignments to process large files, with the goal of doing it efficiently in particular on a machine that could not support storing the entire file contents in RAM. Having said that with the even larger volumes of data today it is still not practical to always load an entire file into memory.

This time I speak of wasn’t that long ago (only 2004), so we had reasonable machines hosting our Linux development environments. From what I recall the hype back then was 64 bit architectures, with the AMD Opteron having only just come out. And we’re still running x86 OS’s, shame.

The subject was focussed on understanding the processing cost of hard drive access and had us calculating latency, seek, and read times. Less of a concern with the speed of Hard Drives today (or even back then) but none the less very useful from a concept of learning fundamentals of the machines. I believe we were dealing with files that were under 1Gb, but obviously restricted from loading the entire file into memory at once. We were also required to incorporate sorting algorithms, no point in just reading and dumping a file, the trick being you couldn’t access the entire file so you had to sort in chunks. But that’s another concept.

The most annoying aspect was having to write code for a compiler that couldn’t handle a code file width greater than 80 characters. So the trick we were taught was to put in some comments at the top counting the spaces. (We would also adjust our terminal window to 80 characters wide).

      *  1         2         3         4         5         6         7

Note in the samples COBOL reserved keywords are capitalised. The use of the full-stop was also a great headache for us. Even tho we were learning C/C++ and already well accustomed to putting semi-colons every where especially after struct/class definitions { }; The trick was actually not every line has a full stop. As a coding style you would continue on the next line for nested/associated sub-calls.

I don’t think there’s a WordPress ‘sourcecode’ tag attribute that will accept language=’COBOL’, so the highlighting won’t be perfect, even tho it’s not perfect for a lot of the WCF and C#3.0 code.

We would always start a code file off with some descriptive information. Of course indented to start at the 7th character position. The first 6 positions were used for line numbers. But it wasn’t mandatory to have line numbers in that region, at least not mandatory for our compiler.

PROGRAM-ID. myCobolFileInput.

Then get right into declaring and opening file handles.


  SELECT file-in
    ASSIGN TO 'input.dat'.

In this example handling a file that’s 80 characters wide too, (79 data, 1 newline \n).

FD file-in.
01 line-in.
  05 data-part.
    10 current-line            PIC X(79).
  05 line-end-marker         PIC X. 

99 end-of-data  PIC XXX.

The main processing block (program loop etc).

    PERFORM 200-open-files.
    PERFORM 300-read-input.
    PERFORM 400-write-output
        UNTIL end-of-data IS EQUAL TO "yes"
    PERFORM 500-close-file.


And the rest:

    OPEN INPUT  employee-details-in.
    MOVE "no" TO end-of-data.

    READ employee-details-in
        AT END MOVE "yes" TO end-of-data.

    DISPLAY current-line.

    CLOSE file-in.

END PROGRAM myCobolFileInput.

No guarantees that’ll run as expected, as I really cannot be bothered even googling what would be required to get a compiler setup to handle this.

Interestingly the book (still on my book-shelf) that we were using for the course Mastering Cobol Programming (Palgrave Master) is available on

I will get back to WCF/WPF material soon.

Published by Nick Josevski

Software Engineer at Octopus Deploy

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: