Cambridge IGCSE 0478 ยท A-Level 9618

File Handling
in Pseudocode

OPENFILE, READFILE, WRITEFILE & CLOSEFILE โ€” the complete Cambridge syntax guide with exam-ready examples.

โšก Quick Summary: File Handling Keywords

  • OPENFILE "name" FOR READ: Opens a file so you can read data from it
  • OPENFILE "name" FOR WRITE: Opens/creates a file to write data (overwrites existing data)
  • OPENFILE "name" FOR APPEND: Opens a file to add new data to the end without erasing existing data
  • READFILE "name", Variable: Reads one line from the file into a variable
  • WRITEFILE "name", Data: Writes data to the file
  • CLOSEFILE "name": Closes the file โ€” always do this at the end!
  • EOF("name"): Returns TRUE if the end of the file has been reached

File Opening Modes

Before you can read from or write to a file in pseudocode, you must open it using OPENFILE with the correct mode. There are three modes:

READ

Opens an existing file to read data from it. Does not change the file contents.

WRITE

Creates a new file or overwrites an existing file. All previous data is lost.

APPEND

Opens an existing file and adds new data to the end, keeping the existing data.

Reading From a File

To read data from a file, you open it in READ mode, then use READFILE to get one line at a time, and loop until the end of file (EOF) is reached.

Reading From a Text File
DECLARE StudentName : STRING

// Step 1: Open the file for reading
OPENFILE "students.txt" FOR READ

// Step 2: Read all lines until end of file
WHILE NOT EOF("students.txt")
    READFILE "students.txt", StudentName
    OUTPUT StudentName
ENDWHILE

// Step 3: Always close the file
CLOSEFILE "students.txt"

Writing to a File

To write data to a file, open it in WRITE mode. Be careful โ€” this will overwrite all existing data in the file.

Writing to a Text File
DECLARE Name : STRING
DECLARE i : INTEGER

// Opens the file โ€” creates it if it doesn't exist
OPENFILE "output.txt" FOR WRITE

// Write 3 names to the file
FOR i โ† 1 TO 3
    OUTPUT "Enter a name:"
    INPUT Name
    WRITEFILE "output.txt", Name
NEXT i

CLOSEFILE "output.txt"
OUTPUT "File saved successfully."

Appending to a File

If you want to add new records to an existing file without losing the old data, use APPEND mode instead of WRITE.

Appending to a Text File
DECLARE NewRecord : STRING

// APPEND preserves existing file contents
OPENFILE "log.txt" FOR APPEND

OUTPUT "Enter new log entry:"
INPUT NewRecord
WRITEFILE "log.txt", NewRecord

CLOSEFILE "log.txt"
OUTPUT "Entry added to log."

Using EOF() to Read an Entire File

The EOF(filename) function returns TRUE when there are no more lines to read. Always check this inside a WHILE loop when reading files of unknown length.

โš ๏ธ Exam Tip: Never use a FOR loop for reading files unless you know the exact number of records. Always use WHILE NOT EOF() when the file length is unknown.

Complete Example: Student Score File

This complete example writes 5 student names to a file, then reads them back and displays each one:

Write, then Read a Student File
DECLARE Name : STRING
DECLARE i : INTEGER

// PART 1: Write 5 student names to file
OPENFILE "class.txt" FOR WRITE
FOR i โ† 1 TO 5
    OUTPUT "Enter student name:"
    INPUT Name
    WRITEFILE "class.txt", Name
NEXT i
CLOSEFILE "class.txt"

// PART 2: Read all names back and display them
OUTPUT "--- Class Register ---"
OPENFILE "class.txt" FOR READ
WHILE NOT EOF("class.txt")
    READFILE "class.txt", Name
    OUTPUT Name
ENDWHILE
CLOSEFILE "class.txt"