OPEN "filename.txt" FOR READ — opens existing file to read from.
OPEN "filename.txt" FOR WRITE — creates/overwrites file to write to.
OPEN "filename.txt" FOR APPEND — opens to add to end of file.
READ "filename.txt", variable — reads next value into variable.
WRITE "filename.txt", data — writes data to the file.
CLOSE "filename.txt" — must close file when finished. Saves data and frees resources.
Reading all records from a file
Use a WHILE loop with ENDOFFILE() to read until the end.
WHILE NOT ENDOFFILE("scores.txt")
Read each record inside the loop.
Process or display each record.
Close the file after the loop.
Always check ENDOFFILE before reading — reading past end of file causes errors.
Common file handling patterns
Write and close: write data in a loop, then close.
Read and process: open, read in loop while not EOF, process each value, close.
Append: open for append, write new data, close — old data preserved.
Always close files — leaving files open can cause data loss or corruption.
Writing to a file
// Save 5 scores to a file
OPEN "scores.txt" FOR WRITE
FOR i = 1 TO 5
score = INT(USERINPUT)
WRITE "scores.txt", score
END FOR
CLOSE "scores.txt"
PRINT "Scores saved."
Reading from a file
// Read and display all scores
OPEN "scores.txt" FOR READ
WHILE NOT ENDOFFILE("scores.txt")
READ "scores.txt", score
PRINT score
END WHILE
CLOSE "scores.txt"
Reading and processing (find average)
OPEN "scores.txt" FOR READ
total = 0
count = 0
WHILE NOT ENDOFFILE("scores.txt")
READ "scores.txt", score
total = total + score
count = count + 1
END WHILE
CLOSE "scores.txt"
IF count > 0 THEN
PRINT total / count
END IF
Exam practice — 5 questions · 12 marks · 1CP2 Paper 1 style
⚡ Write your pseudocode or answer — the AI marks against the Edexcel 1CP2 Paper 1 mark scheme.
1 markENDOFFILE purpose1CP2 Paper 1
Why is ENDOFFILE() used when reading from a file?
A To close the file when reading is complete
B To detect when there is no more data to read, preventing errors from reading past the end
C To count the number of records in the file
D To check if the file exists before opening
4 marksWrite file handling code1CP2 Paper 1
Write pseudocode to: (a) open a file called "names.txt" for reading, (b) read each name and print it with a line number (1, 2, 3...), (c) close the file after reading. (4 marks)
Hint: OPEN for READ → WHILE NOT ENDOFFILE → READ each line → PRINT with counter → increment counter → END WHILE → CLOSE.
+40 XP
1 markAPPEND vs WRITE1CP2 Paper 1
A program opens a file FOR WRITE instead of FOR APPEND. What is the difference?
A FOR WRITE is faster than FOR APPEND
B FOR WRITE overwrites all existing data; FOR APPEND adds to the end, preserving existing data
C FOR APPEND creates a new file; FOR WRITE uses an existing one
D There is no difference
4 marksIdentify the error1CP2 Paper 1
Find and fix the error in this pseudocode:
OPEN "data.txt" FOR READ
WHILE NOT ENDOFFILE("data.txt")
PRINT value
READ "data.txt", value
END WHILE
CLOSE "data.txt"
Hint: Look at the order of READ and PRINT. Which must come first? What happens on the very first iteration?
+40 XP
1 markClose file importance1CP2 Paper 1
Why must a file be closed after use?
A To prevent other programs from reading the data
B To ensure all written data is saved and the file resource is released
C Files close automatically when variables go out of scope