- [Voiceover] To access records in a relative file a relative record number must be provided. Supplying this number allows the record to be accessed directly because the system can use the start position of the file on disk, the size of each record, and the relative record number to calculate the position of the record. In order to demonstrate reading relative files, we need to first create a relative file. Since that cannot be done with a regular text editor, I've written a small COBOL program that reads a sequential file and saves it as a relative file with the necessary key.
On the right is my sequential file of 11 suppliers. Notice each supplier has a unique supplier code as the first two digits. This code will be used to generate the relative key for our relative file. On the left is the program to generate the relative record file. Starting on line 8, we identify the incoming sequential file as line sequential. You can see I'm naming the file "input supplier" and I'm assigning it to supplierseq.dat which is the name of the file on the right.
Then we define the new file we are creating with the organization of relative, the access mode is random, and a relative key is the supplier key. If I scroll down to working storage you'll see that the supplier key is defined as a two digit key. So the numbers on the right, 01 for Acme Videos, 01 will be the supplier key for the first record in the file. Let's scroll down a little more so we can get a better view so we can a better view of the procedure division.
Here we open for input the input supplier, which is our sequential file, and we open for output our supplier file which will be the relative file. We read the first record in the input, then we go down to process file. Here we start by moving to the supplier code, those first two digits, to supplier key. Then we move the entire record to the relative record file name and we finally write that record to our file. If there's an error we print out an error message.
This continues until all records have been processed. This program generated the corresponding relative record file that we need for our second program which reads the data from a new file. Let's open the second program. Here we define one file as input, the new relative record file. Notice it has all the same attributes, organization is relative, access mode is random, and relative key is supplier key. If we scroll down to the procedure devision, we have provided the user with the option of reading the new file sequentially, or providing a supplier code to just display one record.
Remember, relative files can be accessed sequentially or we can use the key to access the records directly. Let's run the program, then we'll come back and take a closer look at the code. When I run the program I get the message do I want to do a direct read, in which case I enter the number 1, or do I want to do a sequential read? Let's start with the sequential read so we can see all the records in the file. So I'm going to type 2 and hit enter. There's all the records that are in my file.
Now I'm going to run it a second time. Since the program and the file are set up to allow me to do a direct read, I'm going to try that. I enter a 1 for direct read and now I need to enter a supplier code. So let's enter 05. You can see 05 in my file is CBS Studios Hollywood, California. Cool, well that works! Let's go back to our program. Starting on line 54, if it is a direct read we display the message "enter supplier code key".
We accept that value into our supplier key directly. Now when I read the input supplier file the computer uses that key to automatically take me to the correct record. I perform 0200 display record and that's the end of the program. If the number I entered was not found, it would display "supplier status" with the corresponding error code. Although my input file was small, using relative records allows me to easily access specific records inside my file without having to read every single one.
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