Cambridge IGCSE · Topic 8.3
IGCSE Computer Science 0478 / Topic 8 / IGCSE

Data that stays.

When a program stops, every variable it used is lost. Files let a program keep data, such as a high score or a list of names, so it can be used the next time the program runs or by another program.

The key idea

Variables are held in main memory and are lost when the program ends. Storing data in a file keeps it permanently in secondary storage, so it can be used again later, by this or another program. In pseudocode: OPENFILE "Names.txt" FOR READ or FOR WRITE, then READFILE to read the next line into a variable, WRITEFILE to write a value, and CLOSEFILE when finished. Opening a file FOR WRITE creates a new file: any existing data in it is lost.

program (in RAM) NameCount lost when theprogram stops Names.txt (storage) AliBeaChen WRITEFILE READFILE data in a file is kept after the program ends
Fig. 1: WRITEFILE copies data from a variable into the file; READFILE copies it back
Section 01

Why use files?

In the lab, choose Write a file, then read it. The program saves three names to Names.txt; the file panel shows the data that would still be there after the program stops.

Examiner trap

"Files save data" earns little. Say that data in variables is lost when the program ends, but data in a file is stored permanently so it can be accessed again later.

Section 02

Writing to a file.

Open the file in WRITE mode, write each item with WRITEFILE, then close it. Each WRITEFILE writes one item, a single value or a whole line of text, as a new line.

DECLARE Name : STRING
DECLARE Count : INTEGER
OPENFILE "Names.txt" FOR WRITE
FOR Count ← 1 TO 3
    INPUT Name
    WRITEFILE "Names.txt", Name
NEXT Count
CLOSEFILE "Names.txt"

The same idea in Python (for comparison only: the exam uses pseudocode)

with open("Names.txt", "w") as file:   # "w" = write mode; the file is closed automatically
    for count in range(3):
        name = input()
        file.write(name + "\n")
Worked example Running it twice
The program above is run with the inputs Ali, Bea and Chen. It is then run again with Dev, Eve and Fay. What does Names.txt contain at the end?
  1. First run: the file holds Ali, Bea, Chen, one per line.
  2. Second run: OPENFILE ... FOR WRITE creates a new file, so the old names are lost.
  3. The three new names are written.
Answer: Dev, Eve and Fay only
Examiner trap

Opening an existing file in WRITE mode wipes it. Only one mode is used at a time, so to read and then rewrite a file, close it and open it again in the other mode.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Why does a program store data in a file?

Quick check+10 XP

What happens when an existing file is opened FOR WRITE?

Quick check+10 XP

In the lab, the names Ava, Ben and Cara are written to Names.txt. Which name does the first READFILE read back?

Quick check+10 XP

Which command should be used when a program has finished with a file?

Section 03

Reading from a file.

Open the file in READ mode. Each READFILE reads the next line of the file into a variable, starting from the first line.

DECLARE HighScore : INTEGER
DECLARE Score : INTEGER
OPENFILE "HighScore.txt" FOR READ
READFILE "HighScore.txt", HighScore
CLOSEFILE "HighScore.txt"
INPUT Score
IF Score > HighScore
  THEN
    OPENFILE "HighScore.txt" FOR WRITE
    WRITEFILE "HighScore.txt", Score
    CLOSEFILE "HighScore.txt"
    OUTPUT "New high score!"
ENDIF

The syllabus example copies one line of text from one file to another:

DECLARE LineOfText : STRING
OPENFILE "FileA.txt" FOR READ
OPENFILE "FileB.txt" FOR WRITE
READFILE "FileA.txt", LineOfText
WRITEFILE "FileB.txt", LineOfText
CLOSEFILE "FileA.txt"
CLOSEFILE "FileB.txt"

The same idea in Python (for comparison only: the exam uses pseudocode)

with open("HighScore.txt", "r") as file:
    high_score = int(file.readline())
Worked example Dry run
HighScore.txt contains 250. The high-score program is run and the player scores 310. What happens?
  1. READFILE reads 250 into HighScore; the file is closed.
  2. Score ← 310. Is 310 > 250? TRUE.
  3. The file is opened FOR WRITE (old contents lost), 310 is written, the file is closed.
  4. New high score! is output.
Answer: The file now contains 310 and New high score! is output
Your turn
The program is run again and the player scores 200. What does the file contain afterwards?
Examiner trap

Always CLOSEFILE when you have finished with a file. Closing it makes sure the data is saved and lets the file be opened again, for example in the other mode.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Which statement opens Scores.txt so that data can be read from it?

Exam style+20 XP

What does READFILE "Data.txt", Item do?

Exam style+20 XP

Which order of commands correctly saves one value to a file?

Exam style+20 XP

A game must remember each player's settings between sessions. Why are the settings stored in a file?

Skill unlocked

File handling, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Why programs use files, and writing pseudocode to open, read, write and close text files.