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:
Stud-Id Surname
8812356 MACKEY TD
8812357
BOBBIN WE
8812358 DALEY FR
8812359 SWINDON GL
8912345 Insert2 ok
8912351 DEWEY AS
8912352
RICHARDSTR
8912353 GORMAN WJ
8912354 FAHY ML
8912355
RYAN GN
8912356 CORKERY DF
8912357 COFFEY SD
8912358
FORD LM
8912359 GRANT
HS
9012351 FLOOD MB
9012352 HUBERT TJ
9012353
JONES VT
9012354 KINGSTONDR
9012355 LANGAN RR
9012356
MORGAN WR
9012357 MANLEY FL
9012358 NORMAN LK
9012359
OTTERMANFD
9123453 Insert3 ok
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:-
- Why is FILLER used in the record descriptions.?
- In the program, the field StudentName is not described as it is in the
record description above. Why is it still a valid description?
- 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?
- 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?
Last updated : March 1997
mailto:michael.coughlan@ul.ie