Inserting records in a Sequential File


READ, WRITE, PERFORM..UNTIL, IF, DISPLAY

Introduction

Using the SeqWrite.Cbl program as a starting point write a program called SeqInsert.Cbl to take a file of new student records (TransIns.Dat) and insert them into the correct order into the student file (Students.Dat).  To do this you will need to create a new file (Students.New) containing the merged records.

Download Students.Dat and save it to theWorkArea directory on drive D:.

Download TransIns.Dat and save it to the same place.
 
 

The File Descriptions

Details of new students are initially recorded in a transaction file (TransIns.Dat).  Records in the file are ordered on ascending StudentId.

Details of existing students are held in the student file (Students.Dat).  Records in this file are also ordered on ascending StudentId.

A program is required which will merge these two files to create a new student file (Students.New) which contains the records of both files.  Records in the new student file must be ordered on ascending StudentId.

The records in the transaction file, the students file and the new file all have the same description.
All three records consist of the following items;
Field
Type
Length
Value
Student Id
9
7
0-9999999
Student Name
.
.
Group
Surname
X
8
-
Initials
X
2
-
DateOfBirth
.
.
Group
Year
9
2
00-99
Month
9
2
01-12
Day
9
2
01-31
Course Code
X
4
-
Grant
9
4
0000-9999
Gender
X
1
M/F
 
 

Further Work

Add code to your program to read the file you have just created and display the StudentId and StudentName from each record.  Display the records as shown in
the example run below:

Sample Solution

When you have written your program and have compiled it and have it working correctly you may wish to compare it with this
sample solution.
 
                                                         WARNING
Do not look at the solution until you have finished your own or at least have made a substantial effort to complete it.
 
 

Examining the sample solution

Compile and run the sample solution provided.  Use the Animator to step through the program and examine the contents of the records as the program executes.
As you examine the program, attempt to answer the following questions:-
  1. Why is FILLER used in the record descriptions.?
  2. In the program, the field StudentName is not described as it is in the record description above.  Why is it still a valid description?
  3. Why are the Student File and Transaction File records filled with "9"'s when the end of file is reached? What problem does this solve neatly?
  4. The READ TransFile statement in the paragraph MergeTheFiles is outside the scope of the second IF statment.  Why is the IF statement written like this?
 
 

Back to COBOL Exercises page To COBOL Exercises Please fill out our evaluation form Evaluation Form Search the CSIS Web Site Search CSIS Web Site
Selectable Site Contents


Last updated : March 1997
e-mail : CSISwebeditor@ul.ie