Learn in your language
Skip to content
IGCSE · Cambridge (CIE) · 0478

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.

Cambridge IGCSE Computer Science 0478 · Programming · 2026–2028 Version 6 · 8.3
Exam scope and assessment notes

This sequence follows Cambridge IGCSE Computer Science 0478, 2026–2028 syllabus Version 6. 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.

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?

Put the idea to work

Worked example

Copy one line of text from Source.txt to Copy.txt.

Show the worked solution
  1. Declare LineOfText as STRING.
  2. Open Source for READ and Copy for WRITE.
  3. 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.

1 · Guided

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
  1. OPENFILE "Scores.txt" FOR READ.
  2. READFILE the file identifier into Score.
  3. 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
2 · Independent

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
  1. Open the file for WRITE.
  2. Use WRITEFILE with Message.
  3. 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
3 · Transfer

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
  1. READ mode does not permit writing.
  2. Close the input file when reading is complete.
  3. 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
Recall without your notes

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.

Keep learning Computer Science

Course scope and assessment

165 original lessons map the listed objectives across all 10 main topics in the 2026–2028 Version 6 syllabus, with worked explanations and guided, independent and transfer practice. This is mapped teaching coverage, not proof of full exam readiness. Three earlier starter lessons remain available but are not counted as syllabus-mapped teaching. Practical programming experience in an executable browser workspace, full timed-paper practice, human subject-teacher review and the separate 2029–2031 syllabus remain outstanding. Practice is self-checked and does not award an official Cambridge grade.

StudyForA · Learn with understanding

Original teaching content and links to official sources. Check your exam year and qualification before using a resource.