SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
AQA 8525 · Section 3.2 · 3.2.7 / 3.2.9

File Handling
Reading & Writing

OPEN · READ · WRITE · CLOSE · endOfFile()

CSZoneAQA GCSE Computer Science 8525
File Operations — AQA 8525

The Four File Commands

CommandPurposeAQA Pseudocode
OPENOpens a file for reading or writingOPEN 'data.txt'
READReads a line from an open fileREAD 'data.txt', line
WRITEWrites a value to a fileWRITE 'data.txt', 'Hello'
CLOSECloses the file when doneCLOSE 'data.txt'
Always CLOSE files:Not closing a file can cause data loss or corruption.
Reading from a File

Reading All Lines Until End of File

OPEN 'scores.txt'
WHILE NOT endOfFile('scores.txt')
  READ 'scores.txt', line
  OUTPUT line
ENDWHILE
CLOSE 'scores.txt'
endOfFile('filename') returns True when there are no more lines left to read. Use it with WHILE NOT to process every line.
Writing to a File

Creating and Appending Files

WRITE (creates/overwrites)
OPEN 'results.txt'
WRITE 'results.txt', 'Ali: 85'
WRITE 'results.txt', 'Beth: 72'
CLOSE 'results.txt'
WRITING IN A LOOP
names ← ['Ali','Beth','Carlos']
OPEN 'out.txt'
FOR i ← 0 TO 2
  WRITE 'out.txt', names[i]
ENDFOR
CLOSE 'out.txt'
Exam Practice

Have a go at this question

AQA-style question
Write pseudocode to open a file called 'names.txt' and read all lines from it, outputting each one. Then close the file.
4 marks
OPEN 'names.txt'
WHILE NOT endOfFile('names.txt')
  READ 'names.txt', name
  OUTPUT name
ENDWHILE
CLOSE 'names.txt'
Key Takeaways

What to Remember

File operations: OPEN → READ/WRITE → CLOSE
endOfFile('file') — returns True when no more data to read
WHILE NOT endOfFile() — standard pattern to read all lines
Always CLOSE the file — prevents data loss