Input/output and file handling
Programs are useful when they communicate with the outside world. They take input (from a user, a file, a sensor) and produce output (to the screen, a file, a printer). On the AQA spec you need to know about user I/O, formatted output, and reading and writing text files.
User input
USERINPUT reads a line from the user. The result is treated as a string by default — so if you need a number, convert it.
USERINPUT name -- string
SET age TO USERINPUT -- string
SET ageInt TO STRING_TO_INT(age)
Output
OUTPUT displays text on screen. You concatenate values using +:
OUTPUT "Welcome, " + name
OUTPUT "Total: " + total
In some languages OUTPUT can take multiple arguments — print(name, age, sep=", ").
Formatted output
Pseudocode rarely formats numbers tightly, but in real languages you can pad, round and align:
OUTPUT "Score: " + ROUND(percentage, 1) + "%"
File handling — text files
A text file stores plain characters (lines separated by newlines). To read or write, you open the file, do operations, and close it.
Writing
OPEN file FOR WRITE "scores.txt"
WRITE file "Aisha,87"
WRITE file "Ben,74"
CLOSE file
OPEN ... FOR WRITEcreates a new file or overwrites an existing one.WRITEappends a single line.CLOSEreleases the file handle.
To append instead of overwrite, use OPEN ... FOR APPEND.
Reading
OPEN file FOR READ "scores.txt"
WHILE NOT EOF(file)
READ file line
OUTPUT line
ENDWHILE
CLOSE file
READreads the next line (or character or block, depending on language).EOF(file)returns true when the end of the file has been reached.- Always close the file when done.
Why file handling matters
- Persistence. Data outlives a single run of the program — saved settings, scores, logs.
- Sharing. Files can be moved, emailed, version-controlled.
- Big data. Reading a file in chunks avoids loading everything into RAM.
CSV (comma-separated values)
A common GCSE pattern: a text file with one record per line, fields separated by commas.
Aisha,87,A
Ben,74,B
Chloe,92,A
To process: read each line, split by ",", store in array or record.
OPEN file FOR READ "scores.csv"
WHILE NOT EOF(file)
READ file line
SET parts TO SPLIT(line, ",")
OUTPUT parts[0] + " scored " + parts[1]
ENDWHILE
CLOSE file
Error handling
Real file operations may fail — file missing, permissions denied, disk full. AQA pseudocode doesn't require try/catch but you should mention checking that the file exists, or that the user has permission, in defensive-programming questions.
⚠Common mistakes— Pitfalls
- Forgetting to close the file — wastes resources, may not flush writes to disk.
- Reading past EOF — always test
EOF(file)before reading. - Mode confusion — opening for WRITE wipes existing content; opening for APPEND adds at the end.
- Treating numbers from a file as integers — they are strings; convert before arithmetic.
- Confusing screen output and file output —
OUTPUTgoes to screen;WRITE filegoes to file.
✦Worked example— Worked example — counting lines
SET count TO 0
OPEN file FOR READ "log.txt"
WHILE NOT EOF(file)
READ file line
SET count TO count + 1
ENDWHILE
CLOSE file
OUTPUT "Total lines: " + count
➜Try this— Quick check
A file scores.txt has lines like "Aisha,87". Write pseudocode to read each line and output the average score.
SET total TO 0
SET count TO 0
OPEN file FOR READ "scores.txt"
WHILE NOT EOF(file)
READ file line
SET parts TO SPLIT(line, ",")
SET total TO total + STRING_TO_INT(parts[1])
SET count TO count + 1
ENDWHILE
CLOSE file
IF count > 0 THEN
OUTPUT "Average: " + (total / count)
ENDIF
AI-generated · claude-opus-4-7 · v3-deep-computer-science