How does a program read from and write to a file?
Read from and write to a text file, including opening, reading line by line, writing and closing a file, so that data persists after the program ends.
A focused answer to AQA GCSE Computer Science 3.2.10, covering reading from and writing to a text file, including opening, reading line by line, writing and closing a file so data persists.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
AQA wants you to read from and write to a text file in a program: opening a file, reading it line by line, writing data to it, and closing it, so that data is kept after the program ends.
Why use files
This is the same volatile-versus-non-volatile idea as RAM and secondary storage: a variable lives in RAM, which is cleared when the program stops, while a file lives on disk or SSD, which keeps its contents. Any program that must remember something for next time (a saved game, a contacts list, a log) writes that data to a file.
Reading from a file
fileRef <- OPEN('scores.txt')
WHILE NOT fileRef.endOfFile()
line <- fileRef.readLine()
OUTPUT line
ENDWHILE
fileRef.close()
The loop is controlled by an end-of-file test so it reads exactly as many lines as the file has, with no assumption about how many that is.
Writing to a file
Opening and closing
Processing a file line by line
The usual pattern for reading a text file is a loop controlled by an end-of-file test. You open the file, then while you are not at the end of the file you read the next line into a variable and process it (output it, search it, add a value to a running total), and when the loop ends you close the file. This pattern reads exactly as many lines as the file contains without needing to know the count in advance, which is why a WHILE loop with an end-of-file condition is used rather than a FOR loop. Processing each line as it is read also avoids loading a huge file into memory all at once.
Combining files with the rest of a program
File handling rarely stands alone; it combines with the other constructs. You might read each line of a scores file (iteration), convert the text to a number (type conversion), compare it with the current best (selection), and write the new high score back to the file (writing). Because input from a file arrives as strings, remember to convert any values you need to calculate with into numbers first. Closing the file at the end then guarantees the written data is saved and the file is released for other programs to use.
Try this
Q1. State one reason a program would write data to a file rather than a variable. [1 mark]
- Cue. So the data persists after the program ends, instead of being lost.
Q2. State why a file should be closed after writing to it. [1 mark]
- Cue. To save the changes and free the file for other use.
Exam-style practice questions
Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AQA 20204 marksWrite AQA-style pseudocode that opens a text file called names.txt, reads and outputs every line, then closes the file.Show worked answer →
A correct solution opens the file, loops until the end of file, reading and outputting each line, then closes it:
fileRef <- OPEN('names.txt')WHILE NOT fileRef.endOfFile() line <- fileRef.readLine() OUTPUT lineENDWHILEfileRef.close()
Markers reward opening the file, an end-of-file controlled loop, reading a line each iteration, outputting it, and closing the file at the end. Omitting the close, or looping forever with no end-of-file test, loses marks.
AQA 20224 marksA program saves a high score to a file. Explain why the score must be written to a file rather than kept in a variable, and explain why the file must be closed after writing.Show worked answer →
The score must be written to a file because a variable only exists while the program is running and is lost when it ends; a file stores the data on secondary storage so it persists and is available the next time the program runs.
The file must be closed after writing because closing flushes and saves any data still buffered, ensuring the score is actually stored, and it frees the file so other programs or parts of the code can use it without conflict.
Markers reward the persistence point (variables are lost, files survive) and the close point (saves the data and releases the file).
Related dot points
- Use the common data types, declare and assign variables and constants, and understand the difference between a variable and a constant.
A focused answer to AQA GCSE Computer Science 3.2.1, covering the common data types, declaring and assigning variables and constants, and the difference between a variable and a constant.
- Use common string-handling operations including length, position, substring, concatenation, and converting between case and between strings and numbers.
A focused answer to AQA GCSE Computer Science 3.2.9, covering common string-handling operations such as length, position, substring, concatenation, case conversion and converting between strings and numbers.
- Use the three programming constructs of sequence, selection and iteration, including definite and indefinite iteration, and nest them.
A focused answer to AQA GCSE Computer Science 3.2.2, covering the three programming constructs of sequence, selection and iteration, the difference between definite and indefinite iteration, and nesting.
- Apply the principles of structured programming, breaking a problem into subroutines with clear interfaces, and explain the benefits of this approach.
A focused answer to AQA GCSE Computer Science 3.2.11, covering the principles of structured programming, breaking a problem into subroutines with clear interfaces, and the benefits of the approach.
Sources & how we know this
- AQA GCSE Computer Science (8525) specification — AQA (2020)