Topic 8 Β· Programming Β· 8.16

File Handling Explained Simply

File handling allows a program to save data so it is still available after the program has stopped.

Invitation

What happens to data when a program closes?

Variables and arrays usually store data in memory while a program is running.

When the program stops, that data may be lost. A text file gives the program somewhere permanent to keep important information.

File handling lets a program save data now and use it again later.
Figure 1
Temporary memory or permanent file?
Program running β†’ data in memory
↓
Program closes β†’ memory data may be lost
↓
Text file β†’ data remains saved
Big Idea

Every file task follows the same three-stage pattern

Before a program can use a file, it must open it. The program then reads or writes the data before closing the file safely.

1 Β· OPENConnect to the file.
β†’
2 Β· READ or WRITEUse or change the data.
β†’
3 Β· CLOSEFinish using the file.
Open β†’ Read or Write β†’ Close.
FutureLogic Bridge

A file is like a filing cabinet

First, you open the correct drawer. Next, you take information out or place information inside. Finally, you close the drawer.

The filename identifies the drawer. The data inside the file is the information being stored.

Filename = drawer label. READFILE = take information out. WRITEFILE = place information in.
Figure 2
The filing-cabinet teaching chip
scores.txt
players.txt Β· OPEN
settings.txt
Open the correct file before using its data.
Worked Example

Read a saved player name from a text file

The program opens the file for reading, stores the data in a variable and then closes the file.

PseudocodeRead from a file
OPENFILE "player.txt" FOR READ
READFILE "player.txt", PlayerName
CLOSEFILE "player.txt"

OUTPUT PlayerName
Follow the order: the filename comes first, followed by the variable that receives the data.
Writing Data

Save a new high score to a text file

The same pattern is used for writing. The file is opened for writing, the value is stored and the file is closed.

PseudocodeWrite to a file
OPENFILE "highscore.txt" FOR WRITE
WRITEFILE "highscore.txt", HighScore
CLOSEFILE "highscore.txt"

READ

Data moves from the file into the program.

WRITE

Data moves from the program into the file.

Exam Tip

Filename first, variable second

File-handling questions often test the correct keyword and the order of the values.

βœ— Incorrect

WRITEFILE HighScore, "scores.txt"

The variable and filename are reversed.

βœ“ Correct

WRITEFILE "scores.txt", HighScore

Filename first, then the value to store.

Full-mark habit: use OPENFILE, READFILE or WRITEFILE, and CLOSEFILE in the correct sequence.
Common Mistake

Do not leave the file open

Closing the file tells the computer that the program has finished using it. It also helps ensure that written data is saved correctly.

Common error: writing READFILE or WRITEFILE without first opening the fileβ€”or forgetting CLOSEFILE at the end.
Summary

Files keep data beyond one program run

1OPENFILE connects the program to a text file.
2READFILE loads data; WRITEFILE saves data.
3CLOSEFILE finishes the operation safely.
Remember: Open β†’ Read or Write β†’ Close.