Ad Banner Placeholder
Cambridge IGCSE Computer Science · 0478
Topic 8: Programming — Part 4
File Handling & Maintainability
File operations
Programs can store data permanently by reading from and writing to files on disk. The standard file operations in Cambridge pseudocode are:
| Statement | Purpose |
|---|---|
| OPENFILE filename FOR READ | Opens an existing file so data can be read from it |
| OPENFILE filename FOR WRITE | Opens a file for writing (creates a new file or overwrites an existing one) |
| READFILE filename, variable | Reads the next item of data from the file into a variable |
| WRITEFILE filename, data | Writes data to the file |
| CLOSEFILE filename | Closes the file — essential to save changes and free system memory |
OPENFILE "Results.txt" FOR READ READFILE "Results.txt", DataVariable CLOSEFILE "Results.txt" OPENFILE "Results.txt" FOR WRITE WRITEFILE "Results.txt", "New Result" CLOSEFILE "Results.txt"
Always call CLOSEFILE after finishing with a file. Closing ensures data is saved to disk and releases the file handle so memory is not wasted.
Maintainability
Maintainability means writing code that is easy for other programmers (or your future self) to read, understand, and update. Good practices include:
| Technique | How it helps |
|---|---|
| Meaningful identifiers | Names such as TotalMarks explain purpose better than X |
| Comments (//) | Explain why code exists; ignored by the compiler/interpreter |
| Modularisation (subroutines) | Breaks the program into smaller procedures and functions that are easier to test and reuse |
| Indentation (4 spaces) | Shows which statements belong inside IF, loops, and subroutines |
// Calculate and display the average of five scores
PROCEDURE ShowAverage()
DECLARE Total : INTEGER
Total ← 0
FOR i ← 1 TO 5
INPUT Score
Total ← Total + Score
NEXT i
OUTPUT Total / 5
ENDPROCEDURE
0/15
Ad Banner Placeholder