Learning Objectives
- Explain why programs store data in files.
- Open and close files in READ and WRITE modes.
- Read and write single items and lines of text.
- Construct a correct file-processing sequence.
Key Terms
- File
- A named collection of data stored for later use.
- Persistent storage
- Storage that retains data after a program ends.
- READ mode
- A file mode that allows data to be read from an existing file.
- WRITE mode
- A file mode that writes a new file and replaces existing contents.
- READFILE
- A command that reads one data item or line into a variable.
- WRITEFILE
- A command that writes a data item or line to a file.

Why Store Data In A File
Variables and arrays hold data while a program is running, but their contents are normally lost when the program ends. A file allows data to be stored persistently and used by a later run or another program.
Files can store records, settings, reports or lines of text. Topic 8 requires understanding the purpose of file storage and using files for reading and writing single data items or a line of text.
File handling is not the same as database processing. Topic 8 focuses on sequential open, read, write and close operations.
Opening A File
A file should be explicitly opened before access. READ mode is used when data will be retrieved. WRITE mode is used when data will be written.
Opening in WRITE mode creates a new file and any existing data in that file is lost according to the Cambridge pseudocode guide. A file should be opened in only one mode at a time.
The file identifier is written as a string filename in the standard examples.
OPENFILE "Input.txt" FOR READ
OPENFILE "Output.txt" FOR WRITE
Reading Data
READFILE reads the next data item or line and assigns it to the variable. The variable type should be appropriate for the stored data.
The exact file contents and number of reads are normally described by the question. Do not invent end-of-file operations beyond the supplied requirements.
DECLARE LineOfText : STRING
OPENFILE "Input.txt" FOR READ
READFILE "Input.txt", LineOfText
OUTPUT LineOfText
CLOSEFILE "Input.txt"
Writing Data
WRITEFILE sends the value to the open file. The file must have been opened in WRITE mode.
Multiple WRITEFILE statements can write several items or lines before closing, subject to the scenario.
DECLARE ReportLine : STRING
ReportLine <- "Class average: 72.4"
OPENFILE "Report.txt" FOR WRITE
WRITEFILE "Report.txt", ReportLine
CLOSEFILE "Report.txt"
Copying A Line
This sequence demonstrates both modes. The source is read, the variable temporarily stores the line, and the destination receives it. Each file is closed when no longer required.
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"
Correct Operation Order
| Stage | Required Action |
|---|---|
| 1 | Open the named file in the correct mode |
| 2 | Read or write using the appropriate command |
| 3 | Process or repeat operations as required |
| 4 | Close the file |
File Safety And Logic
Trying to read from a file opened for WRITE or writing to one opened for READ is incorrect. Closing a file releases it and completes the explicit file operation sequence.
Meaningful variable names such as LineOfText and StudentRecord make the data flow easier to understand. File operations can be placed inside loops when several known items are processed.
Worked Examples
Save Three Names
Question: Write three input names to Names.txt.
- Open Names.txt FOR WRITE before the loop.
- Loop three times.
- Input Name and WRITEFILE it each iteration.
- Close the file after the loop.
Answer: The file is opened once, receives three writes, then is closed.
Read One Score
Question: Read one integer score from Scores.txt and output it.
- Declare Score as INTEGER.
- Open Scores.txt FOR READ.
- READFILE into Score.
- Output Score and close the file.
Answer: OPENFILE “Scores.txt” FOR READ; READFILE “Scores.txt”, Score; OUTPUT Score; CLOSEFILE “Scores.txt”.
Examination Guidance
- Open the file before reading or writing.
- Use the correct mode for the intended operation.
- State that WRITE mode replaces existing data.
- Close every opened file.
- Read into a variable of an appropriate type.
Common Mistakes
- Reading before OPENFILE.
- Writing to a file opened in READ mode.
- Assuming WRITE automatically appends to existing data.
- Forgetting CLOSEFILE.
- Using a literal as the destination of READFILE rather than a variable.
Knowledge Check
1. Why use a file?
2. What modes are required?
3. What does WRITE mode do to existing data?
4. What does READFILE do?
5. Why close a file?