This topic summary covers Knowledge Organiser: File Handling within File Handling for GCSE Computer Science. Revise File Handling in 3.2 Programming for GCSE Computer Science with 15 exam-style questions and 10 flashcards. This is a high-frequency topic, so it is worth revising until the explanation feels precise and repeatable. It is section 7 of 7 in this topic. Use this topic summary to connect the idea to the wider topic before moving on to questions and flashcards.
Knowledge Organiser: File Handling
Key Terms
- File: A named collection of data stored permanently on secondary storage
- open(): Opens a file for reading
- openWrite(): Opens a file for writing — overwrites existing content
- readLine(): Reads the next line from an open file
- writeLine(): Writes a line of text to a file
- endOfFile(): Returns true when there are no more lines to read
- close(): Closes a file after use — must always be called
Must-Know Facts
- Variables are temporary; files provide permanent storage that survives program shutdown
- Files must always be closed after use — leaving them open can cause data loss
- openWrite() overwrites the file; use append mode if you want to add to existing content
- Use endOfFile() in a WHILE loop to read all lines without going past the end
- The three steps are: Open, Read/Write, Close (ORC)
Key Concepts
- Open for reading:
myFile = open("data.txt") - Read all lines:
while NOT myFile.endOfFile() ... line = myFile.readLine() ... endwhile - Write to file:
myFile = openWrite("out.txt") ... myFile.writeLine("text") - Always close:
myFile.close()
Common Mistakes
- Forgetting to close the file: Not calling
close()after reading/writing can cause data loss or file corruption — always close as the final step - Confusing open() and openWrite():
open()is for reading;openWrite()is for writing and will overwrite all existing content — using the wrong one destroys data - Not using endOfFile() in a loop: Reading past the end of a file causes a runtime error — always use a WHILE NOT endOfFile() loop to read all lines safely
- Thinking variables and files work the same way: Variables are temporary (lost when the program ends); files are permanent storage on secondary storage — a key distinction examiners test
- Writing pseudocode file handling without open/close: Exam mark schemes require open, read/write, and close — missing either the open or close step loses marks
Practice questions for File Handling
Which OCR pseudocode command is used to open a file called 'scores.txt' for reading?
Describe how OCR pseudocode reads all lines from a text file, using a WHILE loop. Include the role of endOfFile() in your answer.