IB Computer Science · B2 · Programming
B2.5 File processing
Explain how to open, read from, write to and close a text file, process a file line by line and identify common file-handling errors.
1/6A program must open a file in a chosen mode
- A program must open a file in a chosen mode
- Closing a file saves its data and releases it
- Reading brings data from a text file into the program
- Writing sends data from the program into a file
- A loop can process a file one line at a time
- Most file errors come from a few predictable mistakes
- Check your recall
- Practice: 10 questions
Part 1 of 6: A program must open a file in a chosen mode
A program’s variables vanish when it stops. To keep data between runs, a program stores it in a file, a named block of data on a disk. Before using a file, the program must open it.
Opening links the program to the file. It also sets the file mode, which says what the program may do with the file. Write and append modes usually create a file that does not yet exist.
Common file modes
- Mode
- Read
- Purpose
- Take data into the program
- Existing content
- Left unchanged
- Mode
- Write
- Purpose
- Store new data
- Existing content
- Replaced
- Mode
- Append
- Purpose
- Add data at the end
- Existing content
- Kept
Key terms · tap one for its definition
file
A file is a named collection of data stored on a disk or similar device, which persists after a program ends.
file mode
A file mode is a setting chosen when a file is opened that limits the program to reading, writing or appending.
Part 2 of 6: Closing a file saves its data and releases it
Once the work is done, the program must close the file. Closing ends the link between the program and the file. Other programs can then open it. Closing is a short step that is easy to forget.
When a program writes, the data often waits in a buffer. Closing the file sends any data still waiting to the disk.
If a program forgets to close a file, written data may be lost. The file can also stay locked. Opening and closing therefore always come as a pair.
Key term
buffer
A buffer is a temporary area of memory that holds data on its way to or from a file.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: B · Worked explanation
Part 3 of 6: Reading brings data from a text file into the program
With the file open in read mode, the program can bring its data in. A text file stores readable characters arranged in lines. Each line ends at an end-of-line marker.
The program keeps track of its position in the file. Each read returns the next line, then moves the position forward. When no lines remain, the program has reached the end of file.
Key terms · tap one for its definition
text file
A text file is a file whose contents are stored as readable characters, usually organised into lines.
end of file
The end of file is the point after the last item of a file, where a further read has no data to return.
Part 4 of 6: Writing sends data from the program into a file
Reading brings data in, and writing sends it out. In write or append mode, the program sends text to the file. It adds an end-of-line marker after each item to start a new line.
Numbers must be converted to text before they are written. Data is written after the current position, so append mode adds to the end.
Worked example
Write scores to a file
- Open scores.txt in write mode.
- Write 12, then an end-of-line marker.
- Write 15 and 9 in the same way.
- Close the file so that the buffer is saved.
Answer: The file holds three lines: 12, 15 and 9.
New content reaches the file only after it leaves the buffer. Closing the file makes sure that this happens. Writing inside a loop lets a program store a whole list.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: C · Worked explanation
Part 5 of 6: A loop can process a file one line at a time
Real files can be long, so a loop does the repeated work. The usual pattern is a loop that runs until the end of file. Each pass reads one line, processes it and moves on.
Only one line is held in memory at a time. This approach works for files of any length.
A line often holds several values. A separator, such as a comma, divides them. The program splits the line at each separator to obtain the separate values.
Worked example
Total and mean from a file
- Open scores.txt in read mode. Set total to 0 and count to 0.
- Loop until end of file. Read a line, convert it to a number, add it to total and add 1 to count.
- The lines give and . Then close the file.
- mean = total divided by count, so .
Answer: The total is 56 and the mean score is 14.
Key term
separator
A separator is a character, such as a comma, that marks the boundary between two values on one line.
Part 6 of 6: Most file errors come from a few predictable mistakes
Even a correct loop fails if the file is handled badly. Many of these faults cause a runtime error, which stops the program while it runs. The common causes are listed below.
- Opening a file that does not exist or has a wrong path.
- Reading past the end of file.
- Forgetting to close the file.
- Using write mode when append mode was needed.
- Treating text as a number without converting it.
A program can avoid several of these faults. It can check that a file exists before opening it, and test for end of file before each read.
Key term
runtime error
A runtime error is a fault that occurs while a program is running and normally stops it.
Check your recall[14 marks]
6 written questions. Answer each one in your head or on paper, then open the mark scheme.
- 1.Outline[2 marks]
Outline why a program should close a file after using it.
Show mark schemeHide mark scheme
Model answer
Closing sends any data waiting in the buffer to the file, so it is not lost. It also releases the file so that other programs can open it.
- 1 mark for stating that closing saves data still waiting in the buffer
- 1 mark for stating that closing releases the file for other programs
- 2.Distinguish[2 marks]
Distinguish between opening a file in write mode and in append mode.
Show mark schemeHide mark scheme
Model answer
Write mode replaces the existing content of the file with new data. Append mode keeps the existing content and adds new data at the end.
- 1 mark for stating that write mode replaces existing content
- 1 mark for stating that append mode keeps existing content and adds data at the end
- 3.Describe[4 marks]
Describe how a program can read every line of a text file.
Show mark schemeHide mark scheme
Model answer
The program opens the file in read mode. It then loops until end of file, reading one line on each pass and processing it. When the loop finishes, the program closes the file.
- 1 mark for opening the file in read mode
- 1 mark for using a loop that runs until end of file
- 1 mark for reading and processing one line on each pass
- 1 mark for closing the file after the loop
- 4.Determine[2 marks]
A file holds the lines 6, 14 and 10. A program converts each line to a number, adds it to a total that starts at 0, and counts the lines. Determine the total and the mean.
Show mark schemeHide mark scheme
Model answer
Total . Mean .
- 1 mark for obtaining a total of 30
- 1 mark for obtaining a mean of 10
- 5.State[2 marks]
State two common errors when handling files.
Show mark schemeHide mark scheme
Model answer
Any two of: opening a file that does not exist, reading past the end of file, forgetting to close the file, using write mode instead of append mode, or not converting text to a number.
- 1 mark for stating one valid error
- 1 mark for stating a second valid error
- 6.Explain[2 marks]
Explain why a program should convert a line read from a text file before adding it to a total.
Show mark schemeHide mark scheme
Model answer
A line from a text file arrives as a string. Adding strings joins them as text instead of adding their values, so the program must convert each line to a number first.
- 1 mark for stating that data read from a text file is a string
- 1 mark for explaining that unconverted strings are joined rather than added numerically

Practice this topic
10 quick-fire multiple-choice questions, easy to hard. Tap an answer to see if you're right and the worked explanation. (The written questions with mark schemes are under “Check your recall” above.)
0 right0 of 10 answered
Includes the 2 quick checks in the note above.
EasyIdentify[1 mark]
Show answer and explanation
Answer: D · Worked explanation
MediumCalculate[1 mark]
Show answer and explanation
Answer: A · Worked explanation
MediumIdentify[1 mark]
Show answer and explanation
Answer: D · Worked explanation
MediumPredict[1 mark]
Show answer and explanation
Answer: C · Worked explanation
MediumDetermine[1 mark]
Show answer and explanation
Answer: B · Worked explanation
HardCalculate[2 marks]
Show answer and explanation
Answer: D · Worked explanation
HardExplain[2 marks]
Show answer and explanation
Answer: A · Worked explanation
HardIdentify[2 marks]
Show answer and explanation
Answer: C · Worked explanation