- Reading files sequentially is probably the most common access method for COBOL programs. Files can be both input and/or output from a program. Let's look at this sample program called Employee. Notice that I have the Notepad++ window split, so we can see the employee file along with the program simultaneously. This can be done in Notepad++ using the view menu. You go to View, and then you can choose Move, and Move to other view.
This program is designed to read the employee file, keep a running total of all salaries, and print out a report of employees and the total salary amount. My test file only has eight records in it, but that will be easy for us to double check when we run the program to make sure every record gets read. To read the file, we must assign the file in the environment division. The environment division, starting on line four, more specifically in the file control paragraph, which is located in the INPUT-OUTPUT SECTION.
As you can see in the example on line seven, I am using a select statement to assign the file name that my COBOL program will use. In this case, select Employee File. Then, I assign it to the file name of the file that's on my PC, called empfile.dat. On line eight, which is a continuation of line seven, I have to declare the organization for my file. My file has an organization is line sequential.
I'm using line sequential because my programs run under the cygwin platform, which simulates a Unix environment. If I was running on the main frame and I wanted to read it sequentially, I would just have organization is sequential. Main frame COBOL programs connect to the external file in a slightly different way than what we are using here on the PC version of the GnuCOBOL. It is important to note that the employee file name is only useful inside the COBOL program.
In a main frame program, the external physical file name is attached to this file through the JCL, or the Job Control Language. Let me show you an example of what the statement might look like. Here, I have EMPIN, which is a typical naming convention for COBOL input. EMP for employee and IN for in. DD DSN which is Data Set Name = and then the name of the file on my main frame system is SYSADMIN.EMPLOYEE.DATA,DISP=SHARE.
Since I'm not actually updating the file, I set it to a share mode. Okay, let's get back to our PC program. Once we have the file assigned, we can process the records in these files in the procedure division. Let's scroll down to line 96. Here is the first paragraph in our program. 0100-READ-EMPLOYEES. Notice on line 98, we open the employee file. This step is required before reading any of the records in the file.
And after all the records are processed, it is important to close the file, which is on line 104. This is even more important when writing records to a file. Now that you have seen the steps required to read records sequentially from a file, let's run our sample program so you can get an idea how it works. As you can see in our MFILE.DAT, the first employee name is Joe and the last record is Sherry. So that's a good thing to keep in mind when we run our program we want to make sure that the first record that prints is Joe and the last one is Sherry, along with our total.
Let's switch over. The first thing I want to do is compile my program. So I'm going to do cobc -X and then I give the name of the program, 04_02_employee.cbl. I hit Enter and there are no errors, so now I can run the program ./ and the file name, 04_02_employee with no extension.
Okay, the first employee is Joe Smith, that looks good, and the last employee is Sherry Slattery. And it printed out the total of all the salaries of my employees. The report looks good, we definitely read all records from our file sequentially. So I hope this was a good example to show you how you can read from a sequential file.
This course is designed to help new and experienced programmers alike add COBOL (or add COBOL back) to their skill set. Peggy Fisher shows how to get a COBOL development environment up and running and how to start programming. She reviews COBOL's data types and constants, control structures, file storage and processing methods, tables, and strings. Challenges issued along the way will help you practice what you've learned.
- Downloading and installing Cygwin and GNU COBOL
- Editing, compiling, linking, and running COBOL programs
- Describing data in COBOL
- Working with verbs and expressions
- Using branching
- Reading and writing sequential files
- Updating and deleting records
- Working with relative and indexed files
- Creating and searching tables
- Handling strings