Open, read, write and close files
Use the Cambridge file commands in the correct mode and close each file when it is no longer needed.
Exam scope and assessment notes
This sequence follows Cambridge IGCSE Computer Science 0984, 2026–2028 syllabus Version 4. It covers section 8.3 (File handling) only. Pseudocode examples use the published Cambridge format; the separate 2029–2031 syllabus is not mapped here. Practice is original, self-checked and does not award an official Cambridge grade. Full timed papers, a browser-based programming workspace and human subject-teacher review remain outstanding. Cambridge states that 0984 has the same 2026–2028 subject content as 0478, with 9–1 grading. In June and November, Cambridge lists 0984 in Administrative zone 3 and for centres in Oman and the UAE. Confirm availability for your centre and exam series with Cambridge International. The separately published 2029–2031 syllabus changes to Python 3 only and removes pseudocode; this 2026–2028 route does not cover those changes.
Before you begin
- Be comfortable following an ordered algorithm and tracing variable values. Each lesson introduces the Cambridge pseudocode form it uses.
Work on paper. Try each question before revealing help, and explain why your method works.
Quick readiness check
What must happen before READFILE is used?
What you will learn
- Open a file for reading or writing
- Transfer data with READFILE and WRITEFILE
- Close files after use
Open the file in one mode
Use OPENFILE FileName FOR READ to read data or OPENFILE FileName FOR WRITE to write it. A file should be open in only one mode at a time.
WRITE creates a new file and any existing data can be lost. Decide whether this is intended before selecting the mode.
Read or write, then close
READFILE FileName, Variable reads one data item into a variable. WRITEFILE FileName, Variable writes a data item from a variable.
Use CLOSEFILE FileName when the file is no longer needed. For a line of text, read or write a STRING value; close every file that the routine opened.
Pause and explain
What does OPENFILE "Data.txt" FOR WRITE risk doing?
Worked example
Copy one line of text from Source.txt to Copy.txt.
Show the worked solution
- Declare LineOfText as STRING.
- Open Source for READ and Copy for WRITE.
- Read one line, write it, then close both files.
Answer DECLARE LineOfText : STRING OPENFILE "Source.txt" FOR READ OPENFILE "Copy.txt" FOR WRITE READFILE "Source.txt", LineOfText WRITEFILE "Copy.txt", LineOfText CLOSEFILE "Source.txt" CLOSEFILE "Copy.txt"
Try a topic test
Choose Easy, Normal or Difficult. Save your answers and open worked review after finishing.
Test this topic →From guided practice to a new situation
Write your answer and reasoning first. Use a hint only if you are stuck. The model solution is for self-checking; your written work is not automatically marked.
Write the command sequence to read one INTEGER value from Scores.txt into Score and close the file.
Give me a hint
Open in READ mode before reading.
Compare my reasoning
- OPENFILE "Scores.txt" FOR READ.
- READFILE the file identifier into Score.
- CLOSEFILE the same identifier.
OPENFILE "Scores.txt" FOR READ; READFILE "Scores.txt", Score; CLOSEFILE "Scores.txt".
Look for these in your work
- Correct mode
- Value read
- File closed
Write a short sequence to save Message as one item in Message.txt.
Give me a hint
Open in WRITE mode, write the variable, then close.
Compare my reasoning
- Open the file for WRITE.
- Use WRITEFILE with Message.
- Close the same file.
OPENFILE "Message.txt" FOR WRITE; WRITEFILE "Message.txt", Message; CLOSEFILE "Message.txt".
Look for these in your work
- Write mode
- Variable written
- Closed
A routine opens Input.txt for READ, then writes a result to that same file without closing or changing mode. Identify the problem and give a safe outline.
Give me a hint
A file should be open in one mode at a time.
Compare my reasoning
- READ mode does not permit writing.
- Close the input file when reading is complete.
- Open an output file in WRITE mode, write the result and close it.
Use READ mode for the input file, close it after reading, then open an output file for WRITE, write and close it.
Look for these in your work
- Mode conflict
- Safe sequence
Common mistakes
- Reading from a file opened for WRITE
- Writing before opening the destination file
- Using WRITE when existing contents must be preserved
- Forgetting CLOSEFILE
What can you explain now?
Which command reads one item into a variable?
Compare with the explanation
READFILE
READFILE assigns a data item from an open file to a variable.
After trying it yourself, choose your next review. This is your self-assessment.
Your review choice appears on Today. Sign in to sync it across devices.
Make it stick
What are the main steps in a safe file operation sequence?
If you needed the solution, close it and solve the problem again from a blank page. A correct answer is stronger when you can explain the reason for each step.