📁 Topic 2 · 2.4 Input and Output
2.4b File handling
Edexcel 4CP0 · iGCSE Computer Science · ~10 min read
Notes
──
Video
──
Slides
──
Worksheet
──
Quiz

Why File Handling?

Data stored in variables is lost when a program ends — it only exists in RAM. To persist data between runs, programs must read from and write to files stored on secondary storage (e.g. hard drive, SSD).

File Operations

Edexcel 4CP0 requires knowledge of four file operations:

OperationPurposePseudocode keyword
OpenOpens a file for reading or writingOPEN
ReadReads data from an open file into a variableRECEIVE … FROM FILE
WriteWrites data to an open file (overwrites existing content)SEND … TO FILE
CloseCloses the file once operations are completeCLOSE

Reading from a File

OPEN "scores.txt"
RECEIVE score FROM FILE
SEND "Score read: " & score TO DISPLAY
CLOSE "scores.txt"

Writing to a File

OPEN "scores.txt"
SET score TO 85
SEND score TO FILE
CLOSE "scores.txt"

Appending to a File

Appending adds new data to the end of an existing file without deleting what is already there. This is different from writing, which overwrites the file contents.

# Append a new score without losing old data
OPEN "scores.txt" APPEND
SET newScore TO 92
SEND newScore TO FILE
CLOSE "scores.txt"

Reading an Entire File

OPEN "names.txt"
WHILE NOT end of file DO
RECEIVE name FROM FILE
SEND name TO DISPLAY
END WHILE
CLOSE "names.txt"

File Handling Summary

ModeEffect on existing dataUse when…
WriteOverwrites — old data is lostYou want to create a fresh file or replace content
AppendAdds to the end — old data preservedYou want to add new records without losing existing ones
ReadDoes not change the fileYou only need to retrieve data
📝 Exam Tip: Always CLOSE a file after using it. Failing to close can result in data not being saved correctly. Many exam questions ask you to identify what is missing — it's often the CLOSE statement.
⚠️ Common Mistakes
  • Forgetting to close the file — data may not be written to secondary storage
  • Confusing write and append — writing overwrites all existing data
  • Trying to read a file that hasn't been opened first — always OPEN before RECEIVE
← 2.4a Input, Output and Validation Topic 2 · 2.4 Input and Output Next: 2.5 Operators →
🔒
Pro Content
Subscribe to access all 47 Edexcel iGCSE lessons.
£7.99/month
or £59/year