SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
CAIE 9618 · Paper 4 · Topic 4.2.4

File Handling
& Streams

OPENFILE · READFILE · WRITEFILE · CLOSEFILE · EOF() · Text vs Binary Files

CSZone Cambridge International AS & A Level Computer Science 9618
CAIE File Handling Commands

The Four Key Operations

// Reading from a text 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"

// Writing to a text file
OPENFILE "log.txt" FOR WRITE
WRITEFILE "log.txt", "Error on line 42"
CLOSEFILE "log.txt"

// Append mode
OPENFILE "log.txt" FOR APPEND
WRITEFILE "log.txt", "New entry"
CLOSEFILE "log.txt"
FILE MODES
ModeBehaviour
READOpen existing file to read; file pointer at start
WRITECreate new file OR overwrite existing file completely
APPENDOpen existing file; file pointer at END; adds new data after existing content
EOF() — End of File
EOF("filename") returns TRUE when the file pointer has reached the end of the file
Used in a WHILE NOT EOF loop to read all records without knowing the exact count
Must OPENFILE before calling EOF or READFILE
Text vs Binary Files

Two File Storage Formats

Text Files
Stores data as human-readable ASCII/Unicode characters
Records separated by newline characters
Can be opened and read in any text editor
Larger than binary for numerical data (e.g. number 1000 stored as four characters '1','0','0','0')
Use cases: log files, CSV data, configuration files
Binary Files
Stores data as raw binary (exact memory representation)
Not human-readable without appropriate software
More compact — number 1000 stored in 4 bytes (integer), not 4 ASCII characters
Faster to read/write — no text conversion needed
Use cases: images, audio, video, compiled executables, databases
CAIE 9618 file handling pseudocode covers text files. Binary files access is more commonly covered in context of file organisation (3.1.2).
Exam Practice

Cambridge-style questions

Question 1
Write CAIE pseudocode to: (a) open a file "scores.txt" in READ mode; (b) read and output every line until the end of file; (c) close the file. State what EOF() returns when the end of file has been reached. [4]
1
OPENFILE "scores.txt" FOR READ — correct syntax for opening in READ mode.
1
WHILE loop: WHILE NOT EOF("scores.txt") DO with READFILE "scores.txt", Line and OUTPUT Line inside the loop, terminated by ENDWHILE.
1
CLOSEFILE "scores.txt" after the loop — must close the file when done.
1
EOF() returns TRUE when the end of file has been reached (the file pointer is past the last record).
Common Mistakes

Don't lose easy marks

1
Forgetting to CLOSEFILE — CAIE mark schemes always check for CLOSEFILE at the end. Leaving a file open causes resource leaks and may prevent other programs accessing the file. It's an easy mark to lose.
2
Using WRITE mode when APPEND is needed — WRITE mode overwrites all existing content in the file. If the question asks to add new data to an existing file, APPEND mode must be used. Confusing these destroys existing data.
3
Calling READFILE or WRITEFILE before OPENFILE — the file must be opened first to establish a connection between the program and the file on disk. Reading/writing without opening first is a runtime error. Always follow: OPENFILE → READFILE/WRITEFILE → CLOSEFILE.
Topic Summary — 4.2.4

What You Need to Know

FILE OPERATIONS
OPENFILE "name" FOR READ/WRITE/APPEND. READFILE "name", Variable. WRITEFILE "name", Data. CLOSEFILE "name". Always close the file.
EOF() & MODES
EOF("name") returns TRUE at end of file — use in WHILE NOT EOF loop. READ: read existing. WRITE: overwrite/create. APPEND: add to end without deleting.
TEXT vs BINARY
Text: human-readable ASCII, larger for numbers, any editor. Binary: raw memory format, more compact, faster, not human-readable. CAIE pseudocode uses text file operations.
CSZone

Next Video

4.3.1
Stacks & Queues
Abstract Data Types · LIFO · FIFO · Implementations
Head to CSZone.co.uk for the complete worksheet, quiz, and interactive tools