📘 Paper 2 · Topic 8: Programming
8.4 File Handling
Cambridge IGCSE Computer Science 0478 · ~14 min read · ⭐ Pro

Why File Handling?

Variables and arrays store data temporarily in RAM — when a program ends, that data is lost. File handling allows data to be saved permanently to secondary storage (e.g., a hard drive) so it can be retrieved later by the same or another program.

File Operations in Cambridge Pseudocode

OperationKeywordPurpose
Open for readingOPENFILE "name" FOR READOpen an existing file to read from it
Open for writingOPENFILE "name" FOR WRITEOpen/create file to write data (overwrites)
Open for appendOPENFILE "name" FOR APPENDOpen existing file to add data at the end
Read a lineREADFILE "name", variableRead the next line from the file into variable
Write a lineWRITEFILE "name", dataWrite data to the file
Close a fileCLOSEFILE "name"Close file when done (important!)
End of file checkEOF("name")Returns TRUE when no more data to read

Writing to a File

OPENFILE "scores.txt" FOR WRITE
WRITEFILE "scores.txt", "Alice,95"
WRITEFILE "scores.txt", "Bob,82"
WRITEFILE "scores.txt", "Carol,78"
CLOSEFILE "scores.txt"

Reading from a File

OPENFILE "scores.txt" FOR READ
READFILE "scores.txt", line
OUTPUT line             // Outputs: Alice,95
CLOSEFILE "scores.txt"

Reading All Lines (until EOF)

OPENFILE "scores.txt" FOR READ
WHILE NOT EOF("scores.txt") DO
    READFILE "scores.txt", line
    OUTPUT line
ENDWHILE
CLOSEFILE "scores.txt"

Appending to a File

OPENFILE "scores.txt" FOR APPEND
WRITEFILE "scores.txt", "David,91"
CLOSEFILE "scores.txt"
// David's score is added at the end; previous data preserved

Writing vs Appending

ModeEffect on existing dataUse case
FOR WRITEOverwrites — all old data lostCreating new file or replacing all data
FOR APPENDPreserves old data, adds new data at endAdding new records without deleting old ones
Exam tip: Always show OPENFILE and CLOSEFILE in your answers — marks are awarded for these. Use WHILE NOT EOF() DO to read all records in a file. In exams, the file name is given as a string in quotes. Remember: FOR WRITE overwrites; FOR APPEND adds to end.
⚠️ Common Mistakes
  • Forgetting CLOSEFILE — always close the file when done
  • Using FOR READ when you want to write, or FOR WRITE when reading
  • Using FOR WRITE when you only want to add records — use FOR APPEND to preserve existing data
  • Forgetting NOT in WHILE NOT EOF() — without NOT, the loop runs while the file IS at end (immediately exits)
Video coming soon
Click slide or press arrow keys to navigate
✍️

Worksheet — File Handling

4 questions · 11 marks

Q1Write pseudocode to open a file "names.txt" for writing, write three names to it, then close it. [3]
✅ Mark scheme
OPENFILE "names.txt" FOR WRITE [1]; WRITEFILE "names.txt", "Alice"; WRITEFILE "names.txt", "Bob"; WRITEFILE "names.txt", "Carol" [1]; CLOSEFILE "names.txt" [1]
Q2Write pseudocode to read and output all lines from "data.txt" until the end of the file is reached. [4]
✅ Mark scheme
OPENFILE "data.txt" FOR READ [1]; WHILE NOT EOF("data.txt") DO [1]; READFILE "data.txt", line [1]; OUTPUT line [1]; ENDWHILE; CLOSEFILE "data.txt"
Q3Explain the difference between opening a file FOR WRITE and FOR APPEND. [2]
✅ Mark scheme
FOR WRITE creates/overwrites the file — all existing data is lost [1]; FOR APPEND adds new data to the end of the file while preserving all existing records [1]
Q4Why is it important to close a file after use? [2]
✅ Mark scheme
Any two: Closing ensures all data is actually saved/flushed to disk [1]; Releases the file so other programs can access it [1]; Prevents data corruption [1]; Frees system resources [1]
Quiz — File Handling
Q 1 of 7
Score
/ 7
Click to reveal
TermDefinition
🎯

Mini Test — File Handling

10 minutes · mixed marks

← 8.3 Procedures & Functions Topic 8: Programming Next: 8.5 String Handling →
🔒
Pro Content
Upgrade to access all lessons.
£7.99/month
or £59/year
Subscribe now →