Software Development

How to read file in COBOL

Background

[wp_ad_camp_5]

This article presents a simple COBOL program that reads a flat (text) file, parses the content, and displays them.

Software Environment

  • Windows 7 Professional SP1
  • Visual COBOL for Eclipse Personal Edition 2.2 by Micro Focus
  • Java 1.7 (1.7.0_67 – Windows x86)

The COBOL Program

Here are some descriptions of some lines of codes:

  1. Lines 4 – 5
    • A physical file is mapped to some variable. I’d say not a file handler. It is something like mapping a database table to some Java class using ORM.
      [wp_ad_camp_4]
  2. Line 9
    • Here, CustomerFile becomes a reference. It’s like an instance of an anonymous class in Java passed  to some construct. Or something similar.
  3. Lines 10 – 15
    • Here, we define (or describe) the structure of each line in the file
    • The first 10 characters represent the Customer Number
    • The rest of the 60 characters represent the customer name
      1. First 20 characters represent the customer’s last name
      2. Second set of 20 characters represent the customer’s first name
      3. The rest of the 20 characters represents the customer’s middlename
  4. Line 17, a flag to determine whether the file has reached EOF or not
  5. Line 21, the program opens the file for reading
  6. Line 22, each line in the file is read into the defined “structure” of data type.
    • “Structure” like the construct struct in C/C++.
  7. Line 23, sets the flag to “Y” indicating the program has read the whole content of the file.
  8. Lines 26 – 28, close the file
  9. Line 30, reset the flag and reuse it in line 32
  10. LIne 35, optional. You can use the flag to perform some checks right before the program completes execution.

The Data File

Sample Output

[wp_ad_camp_3]

cobol-read-file

Download the Project

https://www.dropbox.com/s/8x7apkgxasxwjhx/turreta-cobol-read-write%20file.zip?dl=0

 

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like