- In addition to reading files programs often write records to a file. For our sample program, we want to write records to produce a report. As you can see in this program, on line 7, I declare a file called Employee File. That will be my input and on line eleven, I have a file Select Output File which will be my output. My output file will have the name empreport.lpt I have my NotePad++ in split screen mode so you can see the input file.
In order to write records to a file, first format the record in working storage. In this program, I've already defined the heading line, the detail line and two total lines. Let's scroll down and take a look. The heading line starts on line 64 notice the heading line each variable just says filler but on the right-hand side the value specifies what will get printed. employee ID, employee name, start date, and salary. On line 76 is the detail line that matches up with the headings.
In the detail line I define variables for each of the items listed above. If you have a printer spacing chart available, it makes formatting your output much easier. And finally, the two total lines on line 90 and line 96 in my program print out the double lines and the word total. And finally the two total lines starting on line 90 and 96 print out the double lines and the word total along with the total salary for all of the employees.
Let's scroll down to the procedure division. As you can see on line 105, I opened my input file employee file. If you go down a little further to line 111 you'll see open, output, and the name of the file, output file. By adding the parameter output, it tells the computer that we are going to write records to this file. This file is used to write a report that will be printed by a print writer. When using output if a sequential file is used, the existing records will be deleted first, then new records will be inserted.
It is possible to add records to an existing file then use the keyword extends but for our report we want to wipe out the report from the day before and create a new report. The write statement can be used with the from option to directly write records from working storage variables. On line 116 I have the statement, write print line from heading line. Note the from attribute is an optional clause, Next, let's scroll down to 0200 process employees.
Each record is read and the details are moved to the detail report line in working storage to format them for the report. Next, on line 145 the detail report line is written to the output file after advancing two lines. This process continues until all records are read, formatted, and written to the report file. When the program is done reading the file, it writes the final two lines of the report which add formatting and print the total of all salaries. This occurs on lines 124 and 127.
In this example, the program created a report file but the same process is used to create a file of transaction records or any other type of data records.
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