Unable to open this exercise
Common exam pitfall
Students often write INPUT instead of READFILE, or omit CLOSEFILE.
In computer systems, variables held in primary storage (RAM) are volatile; their contents are lost when the program terminates or when power is turned off. To retain data between program runs, programs store information on secondary storage devices in files.
1. What is a Sequential File?
A sequential file stores data records in serial order, one after another from the beginning to the end.
- To access the 50th record in a sequential file, a program must read through records 1 through 49 first.
- EOF is the condition that no unread records remain. It need not be a physical marker stored in the file.
2. Cambridge Pseudocode File Operations
Cambridge pseudocode defines standard primitives for managing sequential text files:
1. Opening a File (OPENFILE)
Before any file operations can take place, the file must be opened in an explicit mode:
OPENFILE <Filename> FOR <Mode>
Mode | Purpose | Behavior with Existing Files |
|---|---|---|
| Used to retrieve data from an existing file. | Generates an error if the file does not exist. Does not alter file contents. |
| Used to create a new file or overwrite an existing file. | If the file already exists, all previous data is erased (overwritten). |
| Used to add new records to an existing file. | Preserves existing data and positions the file pointer at the very end. |
2. Reading from a File (READFILE)
Retrieves the next line or record from an open file and stores it in a declared variable:
READFILE <Filename>, <Variable>
3. Writing to a File (WRITEFILE)
Appends or writes a line or record of data into an open file:
WRITEFILE <Filename>, <DataExpression>
4. Detecting End of File (EOF)
A built-in Boolean function that returns TRUE if the file pointer has reached the end of the file, or FALSE if more records remain to be read:
EOF(<Filename>)
5. Closing a File (CLOSEFILE)
Releases the system handle on the file, flushes any unwritten buffers to physical storage, and makes the file available to other processes:
CLOSEFILE <Filename>
3. The Standard Sequential Read Pattern
Because the number of records in a file is typically not known in advance, a pre-condition WHILE loop combined with NOT EOF(...) is the standard pattern:
DECLARE StudentName : STRING
DECLARE RecordCount : INTEGER
RecordCount <- 0
OPENFILE "ClassList.txt" FOR READ
WHILE NOT EOF("ClassList.txt") DO
READFILE "ClassList.txt", StudentName
OUTPUT "Student: ", StudentName
RecordCount <- RecordCount + 1
ENDWHILE
CLOSEFILE "ClassList.txt"
OUTPUT "Total students registered: ", RecordCount
Why WHILE NOT EOF is used:
- Handles empty files safely: If
ClassList.txtis empty,EOFis immediatelyTRUE. The loop conditionNOT EOFevaluates toFALSE, preventing the program from executingREADFILEon an empty file. - Handles variable lengths: The loop terminates automatically when the last record has been processed, regardless of whether the file contains 5 or 5,000 records.
4. The Standard Sequential Write and Append Patterns
Writing New Data (Overwriting):
OPENFILE "Results.txt" FOR WRITE
WRITEFILE "Results.txt", "Exam Session: June 2026"
WRITEFILE "Results.txt", "Grade: A*"
CLOSEFILE "Results.txt"
Appending Data (Preserving Existing Content):
OPENFILE "AuditLog.txt" FOR APPEND
WRITEFILE "AuditLog.txt", "User Admin logged in at 14:02"
CLOSEFILE "AuditLog.txt"
5. Common Pseudocode Mistakes in File Handling
- Omitting
CLOSEFILE: Failing to close a file can leave buffered data unwritten to disk or lock the file from subsequent access. - Confusing
WRITEwithAPPEND: UsingFOR WRITEwhen intending to add records destroys all previous data in the file. - Using
INPUTorOUTPUTinstead ofREADFILE/WRITEFILE:INPUTandOUTPUTinteract with the user via the console;READFILEandWRITEFILEinteract with persistent storage files. - Reading past the end: Use
WHILE NOT EOFwhen the record count is unknown. AFORloop is suitable when the exact number of available records is known.
AI Socratic Tutor
Need guidance? Ask a question or use the hint below.
AI Socratic Tutor is an All-Access Benefit
Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.
You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.
Exam-Style Question: Sequential File Reading Operations
Fill in the missing Cambridge pseudocode keywords to complete the sequential file reading loop:
DECLARE ItemRecord : STRING
OPENFILE "Inventory.txt" FOR [Space 1]
WHILE NOT [Space 2]("Inventory.txt") DO
[Space 3] "Inventory.txt", ItemRecord
OUTPUT ItemRecord
ENDWHILE
[Space 4] "Inventory.txt"
Interactive exercises for Module 2 and beyond require an active plan.