Adding a Record to a Sequential File –
Explained
In a sequential file, records are stored in order (usually by a key like registerNumber).
Unlike a serial file, you cannot just append new records at the end. You must:
1. Read the old file,
2. Create a new file,
3. Insert the new record in the correct place,
4. Copy the remaining records,
5. Replace the old file with the new one.
Step-by-Step Breakdown of the Pseudocode
1. Declarations
DECLARE studentRecord : TstudentRecord
DECLARE newStudentRecord : TstudentRecord
DECLARE studentFile : STRING
DECLARE newStudentFile : STRING
DECLARE recordAddedFlag : BOOLEAN
- Two record structures: one for existing, one for new.
- Two filenames: one original, one temporary.
- A boolean flag to check if the new record has been added.
2. Open/Create Files
CREATE newStudentFile
OPEN newStudentFile FOR WRITE
OPEN studentFile FOR READ
A new file is created for writing. The existing file is opened for reading.
3. Input the New Record
INPUT [Link]
INPUT [Link]
INPUT [Link]
INPUT [Link]
4. Read & Compare Existing Records
WHILE NOT recordAddedFlag OR BOF(studentFile)
GETRECORD, studentRecord
IF [Link] >
[Link] THEN
PUTRECORD studentRecord
ELSE
PUTRECORD newStudentRecord
recordAddedFlag ← TRUE
Inserts new record at the correct location based on register number.
5. Finish File Transfer
IF EOF(studentFile)
PUTRECORD newStudentRecord
ELSE
REPEAT
GETRECORD, studentRecord
PUTRECORD studentRecord
UNTIL EOF(studentFile)
6. Final Cleanup
CLOSEFILE studentFile
CLOSEFILE newStudentFile
DELETE studentFile
RENAME newStudentFile, studentFile
Replaces the old file with the newly updated file.
Table of Key Identifiers
Identifier Description
studentRecord Record read from student file
newStudentRecord New record to be written
studentFile Original student file name
newStudentFile Temporary new file name
Note: [Link] Append Mode (for Serial Files)
Use this code to open a file in append mode:
myFile = New FileStream("fileName", [Link])
This is suitable for serial files only. For sequential files, use the full read-copy-insert
method.