The shape of the lesson
By the end of the lesson, learners can
- Text files and file modes: A text file is a series of lines read and written one after another as strings.
- Serial and sequential files: Both kinds are read from the beginning, one record after another.
- Random (direct access) files: A random file stores fixed-length records, usually of a user-defined record type.
- Open in the right mode: READ, WRITE (new file, old data lost), APPEND (add at the end), RANDOM (fixed-length records).
- Loop with WHILE NOT EOF(...) and one READFILE per pass; always CLOSEFILE every file you opened.
- Sequential insert: copy to a new file, writing the new record before the first larger key, and handle the end of the file.
- Random: calculate the address, SEEK, then GETRECORD or PUTRECORD; SEEK again before writing back.
One picture every idea returns to
A file must be opened in a mode before use and closed afterwards. READ reads lines from the start; WRITE creates a new file (any existing data is lost); APPEND adds to the end; RANDOM opens a file of fixed-length records for direct access. In a serial file records are stored in the order they were added; in a sequential file they are stored in order of a key field, so inserting a record means copying to a new file. In a random file each record is at an address, often calculated from its key by a hashing function, and SEEK moves the file pointer straight there so GETRECORD and PUTRECORD can read or replace that record.
Forty-five minutes, phase by phase
| Time | Phase | What happens in the room | Grouping |
|---|---|---|---|
| 0 to 5 min | Hook: predict | Open with the question in the title, Records that last.. Data in variables disappears when a program ends; data in a file does not. Put the figure on the board: it is the one picture every idea in the lesson returns to. | Whole class, then pairs |
| 5 to 13 min | Teach and check: Text files and file modes | A text file is a series of lines read and written one after another as strings. Project tab 6 of the lab (Writing and reading text files) and let a student drive it while the class predicts. Your turn: Write pseudocode to add the record for a new member with ID M060 and name Diaz to the end of Members.txt without losing the existing records. | Whole class, then pairs |
| 13 to 21 min | Teach and check: Serial and sequential files | Both kinds are read from the beginning, one record after another. Model the worked example Trace the insertion on the board, one line at a time. | Pairs |
| 21 to 30 min | Teach and check: Random (direct access) files | A random file stores fixed-length records, usually of a user-defined record type. Model the worked example Shift records to make space on the board, one line at a time. Your turn: Using the hashing function ItemCode MOD 500, at which address is item 2318 stored? Which other item code from 2300 to 2999 would collide with it? | Anticipation / Reaction Guide |
| 30 to 36 min | Check | The four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong. | Individual, then whole class |
| 36 to 43 min | Plenary | The exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording. | Individual, random call |
| 43 to 45 min | Exit and homework | Set the worksheet below (6 questions, 23 marks). Learners self-assess against the objectives before they leave. | Individual |
Protect the parts that carry the learning
The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.
Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.
Cut first if the build overruns: the last check in Random (direct access) files, never the check or the plenary.
In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.
Anticipation / Reaction Guide
Before the lesson, students mark five statements true or false. After the teaching they mark them again and justify any change of mind in one sentence each. The statements come from the examiner traps below.
The full structure, with its variations and the other twelve, is on the cooperative-learning page.
What to head off, and how
| Trap learners fall into | Teaching move that pre-empts it |
|---|---|
| Use WHILE NOT EOF(...) before each READFILE, not a FOR loop with a guessed number of lines. | EOF is also TRUE straight away for an empty file, so the loop correctly runs zero times. |
| You cannot insert a line into the middle of a text file in place. | For a sequential file the answer must copy the records to a new file (and then, usually, the new file replaces the old one). |
| Always SEEK before PUTRECORD when updating a record you have just read. | Re-seeking the same address makes certain the changed record replaces the original rather than being written somewhere else. |
Serial, sequential and random file processing: worksheet
6 questions, 23 marks. Written in the style of the papers; no past paper question is reproduced.
Describe the difference between a serial, a sequential and a random file.
The text file Parts.txt holds one part code per line. A code that begins with the letter X is faulty. Write pseudocode for a procedure that reads every line and writes each faulty code to a new file Faulty.txt. It then outputs how many faulty codes were written.
A sequential file holds records with keys 105, 118, 140 and 162. Using the insertion method in this lesson, state the order of records in the new file when a record with key 170 is inserted, and explain which part of the algorithm writes it.
A random file stores records at the address Key MOD 200. (a) Calculate the address for keys 1450 and 2650. (b) State the problem this shows and one way of dealing with it.
A random file Accounts.dat holds records of type AccountRecord, which has fields AccountNo (INTEGER) and Balance (REAL). The record for account number AccNum is at address AccNum MOD 1000. Write pseudocode to add 25.00 to the balance of the account whose number is in AccNum.
A cinema stores seat bookings. The booking office must find and update a single seat instantly, and once a night every booking is printed in seat order. Recommend a file organisation and justify it.
Answer key and marking notes
- Serial: records are stored one after another in the order they were added / no particular order.
- Sequential: records are stored in order of a key field.
- Random: records are stored at addresses (often calculated from the key by hashing) and can be accessed directly without reading earlier records.
- Declare LineOfText : STRING and Count : INTEGER; Count ← 0.
- OPENFILE "Parts.txt" FOR READ and OPENFILE "Faulty.txt" FOR WRITE.
- WHILE NOT EOF("Parts.txt") ... ENDWHILE.
- READFILE "Parts.txt", LineOfText inside the loop.
- IF MID(LineOfText, 1, 1) = "X" THEN WRITEFILE "Faulty.txt", LineOfText and Count ← Count + 1.
- CLOSEFILE both files and OUTPUT Count.
- No existing key is greater than 170, so the record is not written inside the loop.
- All four existing records are copied: 105, 118, 140, 162.
- After the loop, Inserted is still FALSE, so the final IF writes 170 at the end: 105, 118, 140, 162, 170.
- (a) 1450 MOD 200 = 50 (1450 = 7 × 200 + 50); 2650 MOD 200 = 50 (2650 = 13 × 200 + 50).
- (b) Both keys hash to the same address: a collision.
- Store the second record in the next free address (or an overflow area) and search from the calculated address until it is found.
- DECLARE ThisAccount : AccountRecord and calculate Address ← AccNum MOD 1000.
- OPENFILE "Accounts.dat" FOR RANDOM.
- SEEK "Accounts.dat", Address then GETRECORD "Accounts.dat", ThisAccount.
- ThisAccount.Balance ← ThisAccount.Balance + 25.00.
- SEEK "Accounts.dat", Address then PUTRECORD "Accounts.dat", ThisAccount and CLOSEFILE "Accounts.dat".
- A random file, with the address calculated from the seat number.
- Any seat's record can be reached directly with SEEK and updated with PUTRECORD, without reading earlier records, so updates are fast.
- If the address is the seat number itself, reading addresses in order also prints the bookings in seat order (an index or sequential copy could be used otherwise).
6 multiple-choice questions on this lesson
From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.
A program must add new lines to the end of Log.txt each day without losing the lines already in it. Which statement opens the file correctly?
- A
OPENFILE "Log.txt" FOR APPEND - B
OPENFILE "Log.txt" FOR WRITE - C
OPENFILE "Log.txt" FOR READ - D
OPENFILE "Log.txt" FOR RANDOM
A random file stores each record at the address Key MOD 500. At which address is the record with key 3742 stored, if there is no collision?
- A242
- B7
- C258
- D742
Codes.txt holds five lines: X12, A40, X07, B33 and XX1.
DECLARE Line : STRING
DECLARE Count : INTEGER
Count ← 0
OPENFILE "Codes.txt" FOR READ
WHILE NOT EOF("Codes.txt")
READFILE "Codes.txt", Line
IF MID(Line, 1, 1) = "X" THEN
Count ← Count + 1
ENDIF
ENDWHILE
CLOSEFILE "Codes.txt"
OUTPUT CountWhat is output?- A3
- B2
- C4
- D5
Why can a search of a sequential file for a key that is not there stop before reaching the end of the file?
- AThe records are in key order, so once a larger key is read the wanted key cannot appear later
- BSEEK moves the file pointer straight to the address calculated from the key
- CThe records are in the order they were added, so the newest records are read first
- DEOF returns TRUE as soon as a record with the wrong key has been read
In the lesson's code to insert NewLine into the sequential file Members.txt (records M007Lee, M012Shah, M031Novak, M040Ito, M052Grant), the final block
IF Inserted = FALSE THEN
WRITEFILE "NewMembers.txt", NewLine
ENDIFis deleted by mistake. For which value of NewLine would the new record now be missing from NewMembers.txt?- A"M060Diaz"
- B"M001Ames"
- C"M020Patel"
- D"M045Khan"
Addresses 1 to 6 of the random file Queue.dat hold records A, B, C, D, E and F. A programmer tries to move every record one place later:
OPENFILE "Queue.dat" FOR RANDOM
FOR Position ← 1 TO 6
SEEK "Queue.dat", Position
GETRECORD "Queue.dat", Entry
SEEK "Queue.dat", Position + 1
PUTRECORD "Queue.dat", Entry
NEXT Position
CLOSEFILE "Queue.dat"What do addresses 1 to 7 hold afterwards?- AA, A, A, A, A, A, A
- BA, A, B, C, D, E, F
- CA, B, C, D, E, F, F
- DB, C, D, E, F, F, F
Quiz key
APPEND opens the file so new lines are written after the existing data. WRITE creates a new file, so every existing line would be lost, READ only allows lines to be read, and RANDOM is for direct access to fixed-length records, not for adding lines of text.
500 × 7 = 3500 and 3742 - 3500 = 242, so 3742 MOD 500 = 242. 7 is the quotient (3742 DIV 500), 258 is 500 - 242, and 742 only removes the thousands digit, which would be the answer for MOD 1000.
Only the first character is tested, and X12, X07 and XX1 begin with X, so Count is 3. 2 misses XX1, 4 counts every letter X in the file (XX1 has two), and 5 counts every line read.
A sequential file is sorted on its key field, so reading a key greater than the one wanted proves it is absent. SEEK is used for random files, records in the order they were added describe a serial file (which must be read to the end), and EOF only becomes TRUE when there are no more lines to read.
M060 is larger than every existing key, so the condition inside the loop is never true and only the deleted block after the loop would have written it. M001 is written before M007Lee, M020 before M031Novak and M045 before M052Grant, all inside the loop, so those insertions still work.
Pass 1 copies A over B at address 2; pass 2 then reads that A and writes it over C, and so on, so A spreads to every address. A, A, B, C, D, E, F is the intended result, which needs the loop to count down from 6 with STEP -1. A, B, C, D, E, F, F would only copy F, and B, C, D, E, F, F, F would be moving records one place earlier.
What to project
The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.
Slides for this lesson
A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.