Data stored in variables is lost when a program ends — it only exists in RAM. To persist data between runs, programs must read from and write to files stored on secondary storage (e.g. hard drive, SSD).
Edexcel 4CP0 requires knowledge of four file operations:
| Operation | Purpose | Pseudocode keyword |
|---|---|---|
| Open | Opens a file for reading or writing | OPEN |
| Read | Reads data from an open file into a variable | RECEIVE … FROM FILE |
| Write | Writes data to an open file (overwrites existing content) | SEND … TO FILE |
| Close | Closes the file once operations are complete | CLOSE |
Appending adds new data to the end of an existing file without deleting what is already there. This is different from writing, which overwrites the file contents.
| Mode | Effect on existing data | Use when… |
|---|---|---|
| Write | Overwrites — old data is lost | You want to create a fresh file or replace content |
| Append | Adds to the end — old data preserved | You want to add new records without losing existing ones |
| Read | Does not change the file | You only need to retrieve data |