26.1 What is File Handling?
- File handling lets a program store data permanently — the data is NOT lost when the program ends.
- Data is saved to text files on disk instead of staying in RAM.
- Programs can read from and write to files.
- Typical uses: saving student records, storing game scores, logging sensor data.
Key concept: Files must be OPENED before use and CLOSED after use. The four operations are OPENFILE, READFILE / WRITEFILE and CLOSEFILE.
Variables vs Files
Variables (in RAM)
DECLARE Score : INTEGER
Score <- 100
// When the program ends,
// Score is LOST.Files (on disk)
OPENFILE "Scores.txt" FOR WRITE
WRITEFILE "Scores.txt", "100"
CLOSEFILE "Scores.txt"
// "100" is saved forever.Exam tip: If a question asks "how can the data be kept after the program ends?", the answer is always: write it to a file using WRITEFILE.
What is File Handling?
26.2 Opening and Closing Files
OPENFILE <filename> FOR <mode>— opens a file in one of three modes.- READ mode — for reading data from an existing file.
- WRITE mode — for overwriting the file (old data lost).
- APPEND mode — for adding new data to the end (old data kept).
CLOSEFILE <filename>— closes the file (MUST be done when finished).
OPENFILE "Data.txt" FOR WRITE
// ... write data ...
CLOSEFILE "Data.txt"Always close files! If you don't call CLOSEFILE, the operating system may keep the file locked and your data may not be saved to disk. Treat every OPENFILE as a promise to call CLOSEFILE later.
Three modes side by side
READ
OPENFILE "F.txt" FOR READWRITE (overwrite)
OPENFILE "F.txt" FOR WRITEAPPEND (add to end)
OPENFILE "F.txt" FOR APPENDOpening and Closing Files
26.3 Writing to Files
WRITEFILE <filename>, <data>— writes a single line to the file.- In WRITE mode: the entire file is overwritten (old data lost).
- In APPEND mode: new data is added to the end (old data kept).
- The file must already be open in WRITE or APPEND mode before you call
WRITEFILE.
Example — keep asking for names until the user types "STOP"
DECLARE Name : STRING
OPENFILE "Names.txt" FOR APPEND
OUTPUT "Enter name (or 'STOP' to end): "
INPUT Name
WHILE Name <> "STOP" DO
WRITEFILE "Names.txt", Name
OUTPUT "Enter name (or 'STOP' to end): "
INPUT Name
ENDWHILE
CLOSEFILE "Names.txt"Watch out: Using FOR WRITE on an existing file wipes it. If you want to keep old data and add new lines, open with FOR APPEND instead.
WRITE — overwrites
OPENFILE "Log.txt" FOR WRITE
WRITEFILE "Log.txt", "Line 1"
WRITEFILE "Log.txt", "Line 2"
CLOSEFILE "Log.txt"
// Log.txt now contains only
// Line 1 and Line 2.APPEND — keeps old data
OPENFILE "Log.txt" FOR APPEND
WRITEFILE "Log.txt", "Line 3"
CLOSEFILE "Log.txt"
// Old contents kept; "Line 3"
// added at the end.Exam tip: Each WRITEFILE writes one line. To write many lines, put WRITEFILE inside a WHILE or FOR loop — one OPENFILE / CLOSEFILE pair around the loop, not inside it.
Writing to Files
26.4 Reading from Files
READFILE <filename>, <variable>— reads one line into the variable.- The file must be opened with
FOR READfirst. EOF(<filename>)returnsTRUEwhen End Of File is reached.- Each
READFILEcall reads the next line — a hidden file pointer advances automatically.
Example — read and display every name in "Names.txt"
DECLARE Name : STRING
OPENFILE "Names.txt" FOR READ
WHILE NOT EOF("Names.txt") DO
READFILE "Names.txt", Name
OUTPUT Name
ENDWHILE
CLOSEFILE "Names.txt"Key idea: WHILE NOT EOF(<filename>) DO is the standard reading loop. It runs once per line and stops automatically when the file ends — no need to know the line count in advance.
Common mistake: Opening the file with FOR WRITE instead of FOR READ destroys the data and READFILE will not work. Always check the mode matches the operation you intend to perform.
Exam tip: The EOF() check goes in the WHILE condition — not inside the loop body. Calling EOF before READFILE returns FALSE as long as there is at least one unread line.
Reading from Files
26.5 Practical Examples
Five worked examples covering the most common file-handling patterns in IGCSE / O Level exams.
Example 1 — Save student names to a file (APPEND mode)
DECLARE Name : STRING
OPENFILE "Students.txt" FOR APPEND
OUTPUT "Enter student name: "
INPUT Name
WRITEFILE "Students.txt", Name
CLOSEFILE "Students.txt"Example 2 — Read and display all names from a file
DECLARE Name : STRING
OPENFILE "Students.txt" FOR READ
WHILE NOT EOF("Students.txt") DO
READFILE "Students.txt", Name
OUTPUT Name
ENDWHILE
CLOSEFILE "Students.txt"Example 3 — Count how many lines are in a file
DECLARE Line : STRING
DECLARE Count : INTEGER
Count <- 0
OPENFILE "Data.txt" FOR READ
WHILE NOT EOF("Data.txt") DO
READFILE "Data.txt", Line
Count <- Count + 1
ENDWHILE
CLOSEFILE "Data.txt"
OUTPUT "Total lines: ", CountExample 4 — Search for a specific name in a file
DECLARE Name : STRING
DECLARE Target : STRING
DECLARE Found : BOOLEAN
Found <- FALSE
OUTPUT "Enter name to find: "
INPUT Target
OPENFILE "Names.txt" FOR READ
WHILE NOT EOF("Names.txt") AND Found = FALSE DO
READFILE "Names.txt", Name
IF UCASE(Name) = UCASE(Target) THEN
Found <- TRUE
ENDIF
ENDWHILE
CLOSEFILE "Names.txt"
IF Found = TRUE THEN
OUTPUT Target, " was found."
ELSE
OUTPUT Target, " was not found."
ENDIFExample 5 — Copy contents from one file to another
DECLARE Line : STRING
OPENFILE "Source.txt" FOR READ
OPENFILE "Backup.txt" FOR WRITE
WHILE NOT EOF("Source.txt") DO
READFILE "Source.txt", Line
WRITEFILE "Backup.txt", Line
ENDWHILE
CLOSEFILE "Source.txt"
CLOSEFILE "Backup.txt"Key idea: Almost every file-handling task follows the same shape: (1) OPENFILE with the right mode, (2) a loop using READFILE / WRITEFILE, (3) CLOSEFILE. Master this template and you can solve any IGCSE file question.
Practical Examples
26.6 Key Points Summary
Recap of every IGCSE / O Level file-handling concept covered in this topic — memorise these and you will be ready for any file-handling question.
Files store data permanently on disk — not lost when the program ends.
OPENFILE <filename> FOR READ / WRITE / APPEND opens a file.
CLOSEFILE <filename> closes the file — MUST do this when finished.
WRITEFILE <filename>, <data> writes one line to the file.
READFILE <filename>, <variable> reads one line into a variable.
EOF(<filename>) returns TRUE when the end of the file is reached.
WRITE mode overwrites the file; APPEND mode adds to the end.
Always use WHILE NOT EOF(<filename>) DO for reading loops.
Each READFILE reads the NEXT line — the file pointer advances.
Open the file in the right mode: READ for reading, WRITE/APPEND for writing.
Exam tip: When a question asks you to "save" or "store" data, use OPENFILE ... FOR WRITE/APPEND + WRITEFILE. When it asks you to "display all" or "process every line", use OPENFILE ... FOR READ + a WHILE NOT EOF loop. Always close with CLOSEFILE.