Pro Content

Upgrade to access all Cambridge 9618 lessons including records, file handling, and data structures.

Upgrade to Pro →
← Back to Dashboard
🗂️ Paper 2 · 2.2 Data Types & Structures
2.2.3 Records and File Handling
Cambridge 9618 · International A Level Computer Science · ~13 min read
Notes
Video
Slides
Quiz
Worksheet

Records

A record is a composite data type that groups together related fields of different data types under one name. This is equivalent to a struct in C or a class with only fields.

Records are ideal when you want to store multiple pieces of information about a single entity — such as a student (name, age, score) or a product (code, name, price, quantity).

Defining a Record Type

In Cambridge 9618, a record type is declared using TYPE...ENDTYPE:

TYPE Student
  DECLARE name : STRING
  DECLARE age : INTEGER
  DECLARE score : REAL
  DECLARE passed : BOOLEAN
ENDTYPE

Declaring a Record Variable

Once the type is defined, you declare variables of that type and access fields using dot notation:

DECLARE s1 : Student
DECLARE s2 : Student

// Assign values using dot notation
s1.name ← "Alice"
s1.age ← 17
s1.score ← 87.5
s1.passed ← TRUE

// Access a field
OUTPUT s1.name, " scored: ", s1.score
s1 : Student
nameSTRING"Alice"
ageINTEGER17
scoreREAL87.5
passedBOOLEANTRUE

Arrays of Records

Combining arrays and records is very powerful — you can store multiple records in an array:

DECLARE class : ARRAY[1:30] OF Student

// Access the name of the 5th student
OUTPUT class[5].name

// Loop through all students and output names
FOR i ← 1 TO 30
  OUTPUT class[i].name, " : ", class[i].score
NEXT i

File Handling

File handling allows programs to read and write data that persists after the program ends. Cambridge 9618 uses four file operations:

OPENFILE filename FOR READ
Opens an existing file for reading. Must be opened before READFILE is called.
OPENFILE filename FOR WRITE
Opens a file for writing. Creates new file or overwrites existing file.
READFILE filename, variable
Reads the next line from an open file into a variable.
WRITEFILE filename, data
Writes a value (as a line) to an open file.
CLOSEFILE filename
Closes an open file. Should always be done after all operations.
EOF(filename)
Returns TRUE when the end of a file has been reached. Used to stop reading.

Writing to a File

DECLARE line : STRING
OPENFILE "students.txt" FOR WRITE
WRITEFILE "students.txt", "Alice,17,87.5"
WRITEFILE "students.txt", "Bob,16,72.0"
CLOSEFILE "students.txt"

Reading from a File

DECLARE line : STRING
OPENFILE "students.txt" FOR READ
WHILE NOT EOF("students.txt") DO
  READFILE "students.txt", line
  OUTPUT line
ENDWHILE
CLOSEFILE "students.txt"

Arrays vs Records — Key Differences

FeatureArrayRecord
Data types of elementsAll must be the same typeDifferent fields can have different types
Access methodBy integer index: A[3]By field name: s.name
Typical useMultiple values of same type (list of marks)Multiple properties of one entity (a student record)
Cambridge keywordDECLARE x : ARRAY[1:n] OF typeTYPE name ... ENDTYPE
Can they be combined?Yes — ARRAY[1:n] OF RecordType is valid and common
Exam tip: Cambridge 9618 file handling questions often ask you to write pseudocode to read all records from a file. Remember: (1) always OPENFILE before READFILE, (2) use WHILE NOT EOF() to read until end of file, (3) always CLOSEFILE when done. Also — FOR WRITE creates/overwrites; there's no FOR APPEND in Cambridge 9618 pseudocode.
⚠️ Common Mistakes
  • Using . notation before declaring the record variable (DECLARE s1 : Student must come first)
  • Forgetting CLOSEFILE — this loses marks in Cambridge papers
  • Reading a file without checking EOF — causes a runtime error when reading past the end
  • Confusing OPENFILE FOR READ and FOR WRITE — opening for READ when you meant WRITE will fail
  • Thinking records can only have one field — records group multiple fields of different types
  • Using := instead of when assigning record fields
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 2.2.3 Records and File Handling

8 questions · Cambridge 9618 standard

Q1Define a Cambridge 9618 record type called Product with fields: code (STRING), name (STRING), price (REAL), and inStock (BOOLEAN).[4]
✅ Mark scheme
TYPE Product [1]; DECLARE code : STRING [1]; DECLARE name : STRING [1]; DECLARE price : REAL; DECLARE inStock : BOOLEAN [1]; ENDTYPE [1 — check it's present]. Award 4 max.
Q2State the key difference between a record and an array.[2]
✅ Mark scheme
An array stores multiple elements all of the same data type [1]; a record stores multiple fields that can be of different data types [1].
Q3Using the Student record type (name: STRING, age: INTEGER, score: REAL, passed: BOOLEAN), write pseudocode to declare a variable pupil of type Student and assign appropriate values to each field.[5]
✅ Mark scheme
DECLARE pupil : Student [1]; pupil.name ← "any name" [1]; pupil.age ← any integer [1]; pupil.score ← any real [1]; pupil.passed ← TRUE or FALSE [1].
Q4Write Cambridge 9618 pseudocode to open a file called "data.txt", write two lines ("Line 1" and "Line 2") to it, then close it.[4]
✅ Mark scheme
OPENFILE "data.txt" FOR WRITE [1]; WRITEFILE "data.txt", "Line 1" [1]; WRITEFILE "data.txt", "Line 2" [1]; CLOSEFILE "data.txt" [1].
Q5Write Cambridge 9618 pseudocode to read all lines from a file "records.txt" and output each line until end of file is reached.[5]
✅ Mark scheme
DECLARE line : STRING [1]; OPENFILE "records.txt" FOR READ [1]; WHILE NOT EOF("records.txt") DO [1]; READFILE "records.txt", line; OUTPUT line [1]; ENDWHILE; CLOSEFILE "records.txt" [1].
Q6Explain why CLOSEFILE is important and what could happen if it is omitted.[2]
✅ Mark scheme
CLOSEFILE flushes any buffered data to the file and releases the file resource/lock [1]; if omitted, data written to the file may not be saved (lost from buffer), the file may be corrupted, or other programs may be unable to access the file [1].
Q7Write pseudocode for the insertion sort algorithm that sorts an array List[1:N] of integers into ascending order. Use comments to label the key steps: selecting the key element, shifting elements right, and inserting the key.[6]
✅ Mark scheme
Outer FOR loop from 2 to N [1]; Key ← List[i] (selecting the key element) [1]; j ← i - 1 (starting position for shifting) [1]; WHILE j ≥ 1 AND List[j] > Key DO / List[j+1] ← List[j] / j ← j - 1 [1]; ENDWHILE / List[j+1] ← Key (inserting key into correct position) [1]; NEXT i [1]. Award max 6.
Q8Explain why insertion sort runs in O(n) time on a nearly-sorted array, but O(n²) on a reverse-sorted array. Describe the best and worst cases, and state one real-world scenario where insertion sort would outperform quicksort.[4]
✅ Mark scheme
Best case (nearly sorted): each key element is already in (or near) its correct position — the inner WHILE loop runs 0 or very few iterations per outer loop pass, giving O(n) total comparisons [1]; Worst case (reverse sorted): every element must be shifted past all previously sorted elements — the inner WHILE loop runs i−1 times for element i, giving total comparisons of 1+2+…+(n−1) = O(n²) [1]; Real-world scenario: streaming data that arrives nearly in order (e.g. timestamped log entries or an online leaderboard with occasional updates) [1]; insertion sort handles small incremental changes efficiently with minimal overhead, while quicksort's partitioning overhead is wasteful for nearly-sorted small updates [1].
Topic Quiz
Question 1 of 10
You scored
out of 10
Card 1 of 6
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — 2.2.3 Records & Files

10 questions · 10 marks · 10 minutes

← 2.2.2 Arrays
39 of 82 · Cambridge 9618
2.2.4 Abstract Data Types →