27.1 AS Level File Handling Overview
- AS Level (9618) builds on IGCSE file handling with extra features for direct access.
- IGCSE supports only sequential access:
OPENFILE ... FOR READ/WRITE/APPEND,READFILE,WRITEFILE,EOF()andCLOSEFILE. - AS Level adds random access:
OPENFILE ... FOR RANDOMandSEEK. - A file pointer tracks the current position;
SEEKmoves it directly to any record. - Records are 0-indexed (position 0 = first record).
Key concept: AS Level does not replace IGCSE — it adds to it. Every IGCSE statement still works, and SEEK + FOR RANDOM are the new tools for direct access by record number.
IGCSE vs AS Level
IGCSE — sequential only
OPENFILE "Data.txt" FOR READ
WHILE NOT EOF("Data.txt") DO
READFILE "Data.txt", Line
OUTPUT Line
ENDWHILE
CLOSEFILE "Data.txt"AS Level — random access
OPENFILE "Records.dat" FOR RANDOM
SEEK "Records.dat", 5
READFILE "Records.dat", Value
CLOSEFILE "Records.dat"Exam tip: If a question asks you to read or update a record "by number" or "at position N", that is a random access task — use FOR RANDOM + SEEK. Sequential questions use WHILE NOT EOF.
AS Level File Handling Overview
27.2 Random Access Files
- Random access lets you read or write at ANY position — not just sequentially.
- The file pointer tracks the current position in the file.
SEEK <filename>, <position>moves the file pointer to a specific record.- Records are numbered from 0 (position 0 = first record).
- After
SEEK,READFILE/WRITEFILEact on that record.
Example — read record at position 5
OPENFILE "Records.dat" FOR RANDOM
SEEK "Records.dat", 5 // Move to record 5
READFILE "Records.dat", RecordData // Read record at position 5
CLOSEFILE "Records.dat"Key idea: Random access reads ONE record at the pointer position. It does NOT scan the file — you can jump straight to record 1000 without reading the 999 records before it.
Sequential vs Random access
Sequential (IGCSE)
// Must read records 0..N-1
// to reach record N
OPENFILE "F" FOR READ
WHILE NOT EOF("F") DO
READFILE "F", X
ENDWHILE
CLOSEFILE "F"Random (AS Level)
// Jump straight to record N
OPENFILE "F" FOR RANDOM
SEEK "F", N
READFILE "F", X
CLOSEFILE "F"Common mistake: Records are 0-indexed. To read the 1st record, SEEK "F", 0 — not 1. To read the 8th record, SEEK "F", 7. Mixing up 0-indexed (SEEK) and 1-indexed (human) numbering is the most common AS Level file-handling error.
Random Access Files
27.3 Writing Records with SEEK
- You can write to a specific position in a random access file.
SEEKmoves the pointer; the nextWRITEFILEoverwrites the record at that position.- Only the target record is changed — every other record stays the same.
- Ideal for updating individual records (e.g. renaming one student).
Example — update the name of student 4 (record at position 3)
DECLARE StudentName : STRING
OPENFILE "Students.dat" FOR RANDOM
OUTPUT "Enter new name for student 4: "
INPUT StudentName
SEEK "Students.dat", 3 // Position 3 (4th record, 0-indexed)
WRITEFILE "Students.dat", StudentName
CLOSEFILE "Students.dat"Watch out: WRITEFILE after SEEK only works when the file was opened FOR RANDOM. On files opened FOR WRITE or FOR APPEND, SEEK has no effect and WRITEFILE behaves sequentially.
Exam tip: The pattern for updating one record is always: (1) OPENFILE ... FOR RANDOM, (2) get the new value with INPUT, (3) SEEK to the record position, (4) WRITEFILE, (5) CLOSEFILE.
Writing Records with SEEK
27.4 Reading Records with SEEK
- You can read from any position without reading all the preceding records first.
SEEKjumps the pointer; the nextREADFILEreturns just that record.- More efficient than sequential access for large files.
- Use when you know the record number in advance (e.g. student ID, product code).
Example — read record at position 7 (the 8th record)
DECLARE StudentName : STRING
OPENFILE "Students.dat" FOR RANDOM
SEEK "Students.dat", 7
READFILE "Students.dat", StudentName
OUTPUT "Student at position 8: ", StudentName
CLOSEFILE "Students.dat"Key idea: SEEK only moves the pointer — it does not read or write any data. You must always follow it with a READFILE or WRITEFILE to actually move data.
Common mistake: Reading a record at position N after a SEEK to N gives the record at position N — not N+1. The pointer does not advance past the seeked position before the read.
Exam tip: When a question gives you a "record number" in human terms (1, 2, 3, ...), subtract 1 to get the SEEK position. Record 8 in human terms = position 7 in SEEK terms.
Reading Records with SEEK
27.5 Practical Examples
Five worked examples covering the most common AS Level random-access file-handling patterns.
Example 1 — Store 10 student records in a random access file
DECLARE Name : STRING
DECLARE I : INTEGER
OPENFILE "Students.dat" FOR RANDOM
FOR I <- 0 TO 9 DO
OUTPUT "Enter name for student ", I + 1, ": "
INPUT Name
SEEK "Students.dat", I
WRITEFILE "Students.dat", Name
NEXT I
CLOSEFILE "Students.dat"Example 2 — Read a specific student by record number
DECLARE Name : STRING
DECLARE Index : INTEGER
OUTPUT "Enter student number (1-10): "
INPUT Index
Index <- Index - 1 // Convert to 0-indexed
OPENFILE "Students.dat" FOR RANDOM
SEEK "Students.dat", Index
READFILE "Students.dat", Name
OUTPUT "Student: ", Name
CLOSEFILE "Students.dat"Example 3 — Update a specific student's name
DECLARE Name : STRING
DECLARE Index : INTEGER
OUTPUT "Enter student number to update (1-10): "
INPUT Index
Index <- Index - 1 // Convert to 0-indexed
OUTPUT "Enter new name: "
INPUT Name
OPENFILE "Students.dat" FOR RANDOM
SEEK "Students.dat", Index
WRITEFILE "Students.dat", Name
CLOSEFILE "Students.dat"Example 4 — Search through records using SEEK
DECLARE Name : STRING
DECLARE Target : STRING
DECLARE Found : BOOLEAN
DECLARE I : INTEGER
Found <- FALSE
OUTPUT "Enter name to find: "
INPUT Target
OPENFILE "Students.dat" FOR RANDOM
FOR I <- 0 TO 9 DO
SEEK "Students.dat", I
READFILE "Students.dat", Name
IF UCASE(Name) = UCASE(Target) THEN
Found <- TRUE
ENDIF
NEXT I
CLOSEFILE "Students.dat"
IF Found = TRUE THEN
OUTPUT Target, " was found."
ELSE
OUTPUT Target, " was not found."
ENDIFExample 5 — Append a new record to the end of the file
DECLARE Name : STRING
DECLARE Count : INTEGER
Count <- 10 // File currently holds 10 records (positions 0..9)
OUTPUT "Enter new student name: "
INPUT Name
OPENFILE "Students.dat" FOR RANDOM
SEEK "Students.dat", Count // Position 10 = just past last record
WRITEFILE "Students.dat", Name
CLOSEFILE "Students.dat"Key idea: Almost every random access task follows the same shape: (1) OPENFILE ... FOR RANDOM, (2) SEEK to the target position, (3) READFILE or WRITEFILE, (4) CLOSEFILE. Master this template and you can solve any AS Level file question.
Practical Examples
27.6 Key Points Summary
Recap of every AS Level file-handling concept covered in this topic — memorise these and you will be ready for any AS Level random-access file question.
AS Level adds random access file handling on top of the IGCSE sequential statements.
OPENFILE <filename> FOR RANDOM opens a file for random access.
SEEK <filename>, <position> moves the file pointer (0-indexed).
After SEEK, READFILE reads the record at that position.
After SEEK, WRITEFILE overwrites the record at that position.
Records are 0-indexed — position 0 is the first record.
IGCSE uses sequential only (READFILE, WRITEFILE, EOF).
AS Level can do both sequential AND random access.
SEEK only works on files opened FOR RANDOM.
Always CLOSEFILE when done — sequential or random.
Exam tip: When a question asks you to "update record N" or "read record N directly", use OPENFILE ... FOR RANDOM + SEEK. When it asks you to "process every record" or "display all", you can use FOR READ + WHILE NOT EOF (IGCSE style) or a FOR loop with SEEK (AS Level style). Always CLOSEFILE.