Cambridge O Level Computer Science · Syllabus 2210 · Programming
File
What is File?
A named collection of data held on secondary storage so that it persists after the program that created it has finished; it must be opened in READ or WRITE mode before use and closed when no longer needed.
This definition is part of the Programming chapter in Cambridge O Level Computer Science.
File in context
A program is a stored sequence of instructions that manipulates data held in named storage. Topic 8 asks you to do four things with that idea. First, name and type your data correctly: a variable whose value may change, a constant whose value may not, each with one of the five data types INTEGER, REAL, CHAR, STRING, BOOLEAN. Second, control the order in which instructions run using only three structures — sequence, selection (IF, CASE) and iteration (FOR, WHILE, REPEAT). Third, break a long solution into procedures and functions so it stays readable and testable. Fourth, hold many related values in an array and hold them beyond the end of the run in a file. Everything else in this chapter is detail hanging off those four moves.
Variables and arrays live in main memory, which is volatile and is released when the program ends — so anything held only there is lost. A file is stored on secondary storage, so the data still exists after the program has finished and can be read again on a later run, by this program or another one. It is good practice to explicitly open a file, stating the mode of operation, before reading from or writing to it. The two modes are READ, for data to be read from the file, and WRITE, for data to be written to it — and in WRITE mode a new file will be created and any existing data in the file will be lost. A file should be opened in only one mode at a time, and files should be closed when they are no longer needed.
Reading an algorithm that does not work, saying what is wrong with it and correcting it is a skill this topic leans on: the syllabus expects candidates to have practical programming experience, and every construct in Chapter 8 can be got subtly wrong. Two kinds of defect appear. A syntax defect breaks the rules of the notation: == for equality, a missing ENDIF, CALL in front of a function, infix MOD. A logic defect is perfectly legal pseudocode that does the wrong thing: an uninitialised total, a loop that runs one time too many, a counter outside its guard, a file opened in the wrong mode. Logic defects are much harder to spot by reading — which is why you trace.
Common mistakes with File
- M26. “Data in a file disappears when the program finishes.” TruthExactly backwards. Variables and arrays live in main memory and are lost. A file is on secondary storage precisely so the data survives the end of the program. That is the purpose— and it is what an 8.3.1 question is asking you to explain.
- M27. “FOR WRITE adds to the end of the file.” TruthOpening a file FOR WRITE creates a new file, and any existing data in the file is lost — at the moment of opening, before any WRITEFILE runs. There is no APPEND modein this specification. To add to existing data: read it all in, then write it all back out with the addition.
- M28. “You can read a file without opening it first.” TruthA file must be opened, stating the mode, before reading from or writing to it — and a file should be opened in only one mode at a time. It should then be closed when it is no longer needed. Orderopen → read or write → process if required → close.
Examiner tips on File
- Read the closed lists as a promise in both directions. You will never be asked for a FOR EACH loop, a SWITCH, a string .split(), an APPEND file mode or a fourth parameter — none of those is in Topic 8. Equally, every single item that is listed is fair game, including the ones students skip: ^, <>, NOT, ROUND, RANDOM, 2D arrays and writing a line of text to a file.

