| Operation | Syntax (syllabus) | Effect |
|---|---|---|
| OPEN | OPEN <filename> FOR <mode> |
Creates a file handle and sets the access mode (r, w, a, r+, w+, a+). |
| READ | READ <variable> FROM <file> |
Gets the next record/byte from the file. |
| WRITE | WRITE <variable> TO <file> |
Writes a record/byte to the file at the current pointer. |
| CLOSE | CLOSE <file> |
Closes the handle, flushing buffers and freeing resources. |
FOR <mode> means)| Mode | Purpose (syllabus) | Effect on an existing file |
|---|---|---|
r |
Read only | File must already exist; otherwise an error occurs. |
w |
Write only – create or truncate | If the file exists it is cleared; if not, a new empty file is created. |
a |
Append – write only, start at end | If the file exists data is added to the end; otherwise a new file is created. |
r+ |
Read and write | File must exist; reading and writing start at the beginning. |
w+ |
Read and write – create or truncate | Existing file is cleared; a new file is created if necessary. |
a+ |
Read and write – append | Reading can start anywhere; every write adds data to the end of the file. |
1 OPEN "students.txt" FOR r
2 WHILE NOT EOF
3 READ line FROM file
4 *process line here*
5 END WHILE
6 CLOSE file
1 OPEN "data.bin" FOR r
2 SET FILE POINTER TO 100 *move to byte 100*
3 READ value FROM file
4 CLOSE file
1 OPEN "image.bin" FOR r
2 READ BYTE b FROM file *reads a single byte*
3 *use b as required*
4 CLOSE file
1 OPEN "log.txt" FOR w
2 WRITE "Program started" TO file
3 WRITE "User logged in" TO file
4 WRITE "Program finished" TO file
5 CLOSE file
1 OPEN "log.txt" FOR a
2 WRITE "New session at " + TIME() TO file
3 CLOSE file
1 OPEN "output.bin" FOR w
2 WRITE BYTE 0xFF TO file *writes a single byte*
3 WRITE BYTE 0x00 TO file
4 CLOSE file
Even if an error occurs, the file must be closed. A simple “finally” style structure is acceptable in the syllabus.
1 IF FILE_EXISTS("data.txt") THEN
2 OPEN "data.txt" FOR r
3 TRY
4 READ line FROM file
5 *process line*
6 CATCH
7 DISPLAY "Read error"
8 FINALLY
9 CLOSE file
10 END TRY
11 END IF
r mode and the file does not exist.IF FILE_EXISTS("…") THEN … before opening, or open with w/a to create a new file.EOF before a READ operation (as in the sequential‑read loop).INT() or STR().*--- 1. Read existing scores (if the file exists) --------------------*
IF FILE_EXISTS("scores.txt") THEN
OPEN "scores.txt" FOR r
scores ← EMPTY LIST
WHILE NOT EOF
READ line FROM file *format: name,score*
SPLIT line BY "," → name, scoreStr
scores.APPEND( name , INT(scoreStr) )
END WHILE
CLOSE file
END IF
*--- 2. Add a new record --------------------------------------------*
newName ← "Alice"
newScore ← 87
scores.APPEND( newName , newScore )
*--- 3. Write all records back – overwrite the file -----------------*
OPEN "scores.txt" FOR w
FOR EACH rec IN scores
WRITE rec.name + "," + STR(rec.score) TO file
END FOR
CLOSE file
OPEN a file before READ or WRITE.FOR <mode> – it decides whether the file is created, cleared, or appended to.WRITEn (text vs. binary, line delimiters, separators).CLOSE; it finalises the operation and frees resources.EOF before each READ to avoid reading past the end.FILE_EXISTS when opening a file in r mode.TRY … CATCH … FINALLY structure so the file is always closed.r, w, a, r+, w+, a+) and their effect on an existing file.EOF testing.w and a examples).CLOSE is essential, and give an example of closing a file in a FINALLY block.Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.