Sequential File Handling (OPENFILE, READFILE, WRITEFILE)

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

READ

Used to retrieve data from an existing file.

Generates an error if the file does not exist. Does not alter file contents.

WRITE

Used to create a new file or overwrite an existing file.

If the file already exists, all previous data is erased (overwritten).

APPEND

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:

  1. Handles empty files safely: If ClassList.txt is empty, EOF is immediately TRUE. The loop condition NOT EOF evaluates to FALSE, preventing the program from executing READFILE on an empty file.
  2. 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

  1. Omitting CLOSEFILE: Failing to close a file can leave buffered data unwritten to disk or lock the file from subsequent access.
  2. Confusing WRITE with APPEND: Using FOR WRITE when intending to add records destroys all previous data in the file.
  3. Using INPUT or OUTPUT instead of READFILE / WRITEFILE: INPUT and OUTPUT interact with the user via the console; READFILE and WRITEFILE interact with persistent storage files.
  4. Reading past the end: Use WHILE NOT EOF when the record count is unknown. A FOR loop is suitable when the exact number of available records is known.

AI Socratic Tutor

All-Access
Sign in or create an account. AI Tutor is an All-Access benefit.
Sign Up

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

+35 XP

Fill in the missing Cambridge pseudocode keywords to complete the sequential file reading loop:

PSEUDOCODE
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"
Word Bank / Available Cards (Drag into spaces or tap to place)

All-Access Required

Interactive exercises for Module 2 and beyond require an active plan.