Scribe logoScribeNotes & practice
Sign inStart free
  1. Notes
  2. ›IB Computer Science
  3. ›B2 Programming
  4. ›B2.5 File processing

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.

  • 5 min read
  • 6 parts
  • 10 questions
  • Draft
Start a Quick 5 on this topic→Jump to the 10 questions
1/6A program must open a file in a chosen mode
  1. 1A program must open a file in a chosen mode
  2. 2Closing a file saves its data and releases it
  3. 3Reading brings data from a text file into the program
  4. 4Writing sends data from the program into a file
  5. 5A loop can process a file one line at a time
  6. 6Most file errors come from a few predictable mistakes
  7. Check your recall
  8. Practice: 10 questions
1

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

ModePurposeExisting content
ReadTake data into the programLeft unchanged
WriteStore new dataReplaced
AppendAdd data at the endKept
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

Common mistake

Opening an existing file in write mode erases what it holds. Choose append when the old data must stay.

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 1 done

2

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.

Figure 1. Every file use follows the same order: open, read or write, then close.

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.

Part 2 done

Quick check

One question on the part you just read

Easy

Identify the file mode that keeps the existing content and adds new data at the end of a file.
Show answer and explanation

Answer: B · Worked explanation

Append mode leaves what is already in the file and places new data after it. Write mode replaces the existing content. Read mode only takes data into the program and cannot add anything. End of file is not a mode at all, because it names the point where no more data can be read.
3

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.

Exam tip

Data read from a text file arrives as a string. Convert it to a number before adding or comparing it.

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 3 done

4

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

Write the scores 12, 15 and 9 to a new file, one per line.
  1. 1
    Open scores.txt in write mode.
  2. 2
    Write 12, then an end-of-line marker.
  3. 3
    Write 15 and 9 in the same way.
  4. 4
    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.

Part 4 done

Quick check

One question on the part you just read

Easy

State what reaching the end of file means.
Show answer and explanation

Answer: C · Worked explanation

The end of file is the point after the last item, where a further read has no data to return. A loop that processes a file tests for this point so that it stops at the right time. Closing a file, writing a last line and deleting the file are separate events that do not mean the same thing.
5

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

A file scores.txt holds four lines: 12, 15, 9 and 20. Calculate the total and the mean score.
  1. 1
    Open scores.txt in read mode. Set total to 0 and count to 0.
  2. 2
    Loop until end of file. Read a line, convert it to a number, add it to total and add 1 to count.
  3. 3
    The lines give total=12+15+9+20=56\mathrm{total}=12+15+9+20=56total=12+15+9+20=56 and count=4\mathrm{count}=4count=4. Then close the file.
  4. 4
    mean = total divided by count, so mean=564=14\mathrm{mean}=\frac{56}{4}=14mean=456​=14.

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 5 done

6

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.

Exam tip

Name the error and its cause, such as reading past the end of file with no end-of-file test.

Key term

runtime error

A runtime error is a fault that occurs while a program is running and normally stops it.

Note read

Check your recall[14 marks]

6 written questions. Answer each one in your head or on paper, then open the mark scheme.

  1. 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. 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. 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. 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 =6+14+10=30=6+14+10=30=6+14+10=30. Mean =303=10=\frac{30}{3}=10=330​=10.

    • 1 mark for obtaining a total of 30
    • 1 mark for obtaining a mean of 10
  5. 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. 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.

3.

Easy·Identify[1 mark]

Identify the term for a temporary area of memory where data waits before it is written to a file.
Show answer and explanation

Answer: D · Worked explanation

A buffer holds data on its way to or from a file, and closing the file sends the waiting data on. A separator divides values on a line, a file mode sets what the program may do with a file, and a text file is the file itself, so none of them describes temporary memory.
4.

Medium·Calculate[1 mark]

A file holds the numbers 8, 5 and 12 on separate lines. A program reads each line, converts it to a number and adds it to a total that starts at 0. Calculate the final total.
Show answer and explanation

Answer: A · Worked explanation

The total is 8+5+12=258+5+12=258+5+12=25. The value 17 leaves out the first line, which happens if the loop skips the first read. The value 3 is the number of lines, not their sum. The result 8512 comes from joining the lines as text without converting them to numbers.
5.

Medium·Identify[1 mark]

A loop calls read line again after the last line has been read, and it has no end-of-file test. Identify the fault.
Show answer and explanation

Answer: D · Worked explanation

Without a test for end of file, the loop keeps asking for lines when none remain, so it reads past the end. That can cause a runtime error. Append mode, closing the file twice and a missing separator are different problems that do not explain a loop running on after the last line.
6.

Medium·Predict[1 mark]

A file called log.txt holds three lines of old data. A program opens it in write mode, writes one new line and closes it. Predict how many lines the file now holds.
Show answer and explanation

Answer: C · Worked explanation

Write mode replaces the existing content, so the three old lines are erased and only the new line remains. Four lines would be the result in append mode, which keeps old data. Three lines would mean nothing was written, and zero would mean the new line was lost.
7.

Medium·Determine[1 mark]

One line of a file reads Ana,11,12 and the separator is a comma. Determine how many values result when the line is split at each separator.
Show answer and explanation

Answer: B · Worked explanation

Two separators divide the line into three values: Ana, 11 and 12. The value 1 would mean the line was not split at all. The value 2 counts the separators instead of the values. The value 9 counts every character in the line, which is not what splitting at separators produces.
8.

Hard·Calculate[2 marks]

A file holds the lines 4, 9, 7, 8 and 12. A program adds the lines and counts them, then calculates mean = total divided by count. Calculate the mean.
Show answer and explanation

Answer: D · Worked explanation

The total is 4+9+7+8+12=404+9+7+8+12=404+9+7+8+12=40 and the count is 5, so the mean is 405=8\frac{40}{5}=8540​=8. The value 10 divides by 4, which happens when one line is missed in the count. The value 40 is the total, not the mean, and 5 is only the number of lines.
9.

Hard·Explain[2 marks]

A program writes the numbers 3 and 4 to a file in a loop and then ends without closing the file. Later the file is empty. Explain the most likely cause.
Show answer and explanation

Answer: A · Worked explanation

Written data often waits in a buffer, and closing the file sends it to the disk. Without that step the data can be lost, leaving an empty file. Append mode keeps old data rather than erasing it. End of file applies to reading, and missing commas would change the layout of the data but not empty the file.
10.

Hard·Identify[2 marks]

Identify the sequence that correctly adds up every number stored one per line in a text file.
Show answer and explanation

Answer: C · Worked explanation

The total must start at 0 once, before the loop, and the file must be open in read mode while lines are read. Setting total to 0 on every pass would discard the earlier lines. Write mode would erase the numbers, and closing before the loop means there is no open file to read from.

Keep going

Next topicB3.1 OOP with a single classPrevious topicB2.4 Programming algorithms

More in B2 · Programming

  1. B2.1Programming fundamentalsNotes · 10 questions
  2. B2.2Data structuresNotes · 10 questions
  3. B2.3Programming constructsNotes · 10 questions
  4. B2.4Programming algorithmsNotes · 10 questions
  5. B2.5File processingNotes · 10 questions

All IB Computer Science topics →

Draft: written by Scribe and checked automatically, not yet by a subject reviewer.

Practice questions are original, written by Scribe and checked automatically for correctness.

Topic codes follow the official numbering. Not endorsed by IB Diploma Programme or any exam board.

On this page

0/6 read

  1. 1A program must open a file in a chosen mode
  2. 2Closing a file saves its data and releases it
  3. 3Reading brings data from a text file into the program
  4. 4Writing sends data from the program into a file
  5. 5A loop can process a file one line at a time
  6. 6Most file errors come from a few predictable mistakes
  7. Check your recall
  8. Practice: 10 questions
All coursesScribeHow it worksPricingPrivacyTerms

Notes and questions are written by Scribe in our own words. Topic codes follow the official course numbering so you can find your place. Not produced or endorsed by the IB, College Board, AQA, OCR, Pearson Edexcel or any exam board.

© 2026 Scribe