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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CustomerFile ASSIGN TO "20141111-235959-CN-BEIJ.dat" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD CustomerFile. 01 CustomerDetails. 02 CustomerId PIC X(10). 02 CustomerName. 03 Lastname PIC X(20). 03 Firstname PIC X(20). 03 Middlename PIC X(20). WORKING-STORAGE SECTION. 01 END-OF-FILE PIC Z(1). PROCEDURE DIVISION. Begin. OPEN INPUT CustomerFile READ CustomerFile AT END MOVE 1 TO END-OF-FILE END-READ IF END-OF-FILE = 1 CLOSE CustomerFile END-IF MOVE 0 TO END-OF-FILE. PERFORM UNTIL END-OF-FILE = 1 DISPLAY CustomerId SPACE Lastname SPACE Firstname READ CustomerFile AT END MOVE 1 TO END-OF-FILE END-READ END-PERFORM STOP RUN. |
Here are some descriptions of some lines of codes:
- 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]
- 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.
- 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.
- 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
- First 20 characters represent the customer’s last name
- Second set of 20 characters represent the customer’s first name
- The rest of the 20 characters represents the customer’s middlename
- Line 17, a flag to determine whether the file has reached EOF or not
- Line 21, the program opens the file for reading
- Line 22, each line in the file is read into the defined “structure” of data type.
- “Structure” like the construct struct in C/C++.
- Line 23, sets the flag to “Y” indicating the program has read the whole content of the file.
- Lines 26 – 28, close the file
- Line 30, reset the flag and reuse it in line 32
- LIne 35, optional. You can use the flag to perform some checks right before the program completes execution.
The Data File
1 2 3 4 | 1111111111LLLLLLLLLLLLLLLLL001FFFFFFFFFFFFFFFFF001MMMMMMMMMMMMMMMMM001 2222222222LLLLLLLLLLLLLLLLL002FFFFFFFFFFFFFFFFF002MMMMMMMMMMMMMMMMM001 3333333333LLLLLLLLLLLLLLLLL003FFFFFFFFFFFFFFFFF003MMMMMMMMMMMMMMMMM001 4444444444LLLLLLLLLLLLLLLLL004FFFFFFFFFFFFFFFFF004MMMMMMMMMMMMMMMMM001 |
Sample Output
[wp_ad_camp_3]
Download the Project
https://www.dropbox.com/s/8x7apkgxasxwjhx/turreta-cobol-read-write%20file.zip?dl=0