Ch 4 · 8.3 File Handling · O Level Computer Science 2210

Topic 4 of 4 in Programming — O Level Computer Science 2210 — 39 lessons.

Introduction

Variables and arrays are lost when a program ends. To keep a high-score table or a list of students for next time, the program must save the data in a file. (Syllabus section 8.3.)

Key ideas — Why store data in a file?

Data in variables and arrays is held in RAM and is lost when the program closes (or the computer is switched off). Storing data in a file on secondary storage:

🧑‍🏫 Teacher's explanation

Imagine you are playing a video game. While you are playing, your current score, position, and inventory are stored in the computer's 'short-term memory' (RAM). If you suddenly pull the plug on your console, all that progress vanishes because RAM is volatile.

📖 Definitions

🔍 Worked example — Easy

A program stores a user's high score in a variable highScore. If the program ends, the variable is lost. Explain why saving this to a file is necessary.

🔍 Worked example — Standard

A system needs to store a list of 10,000 student names. Why is using a file on secondary storage better than keeping them all in an array in RAM?

🔍 Worked example — Exam-style

A programmer writes a script that creates a file named 'log.txt'. The script writes the string 'Error 404' to it. What are the three primary reasons for using this file instead of just printing the error to the screen?

⚠️ Common errors

🧠 Quick check 1

🧠 Quick check 2

🎯 Exam question

(a) Explain why the programmer should use a file on secondary storage rather than just using variables in RAM to store the quantities. [3] (b) State one advantage of storing this data in a file rather than just displaying it on the screen. [1] [4 marks]

Key ideas — File handling in pseudocode

A file should be opened in only one mode at a time, and closed when it is no longer needed.

🧑‍🏫 Teacher's explanation

Think of a computer program like a person working at a desk. The RAM (memory) is like the surface of the desk; it is very fast to reach things on the desk, but once you leave the room (turn off the computer), everything on the desk disappears. A file is like a filing cabinet in the corner. It takes a little longer to walk over and open a drawer, but whatever you put in there stays there even when you go home and come back the next day.

📖 Definitions

🔍 Worked example — Easy

A program needs to save a user's age. The age is 25. Write the pseudocode to create a file named "age.txt" and store the value.

🔍 Worked example — Standard

A file "data.txt" contains the text "Hello World". Write pseudocode to read this line and output it to the screen.

🔍 Worked example — Exam-style

Trace the following pseudocode and determine the final value of the variable 'Total'. Assume "nums.txt" contains the values 10 and 20 on separate lines.\n\nDECLARE Total : INTEGER\nTotal arrow 0\nOPENFILE "nums.txt" FOR READ\nWHILE NOT EOF("nums.txt")\n DECLARE Val : INTEGER\n READFILE "nums.txt", Val\n Total arrow Total + Val\nENDWHILE\nCLOSEFILE "nums.txt"

⚠️ Common errors

🧠 Quick check 1

🧠 Quick check 2

🎯 Exam question

Key ideas — In Python

("w" overwrites the file; "a" appends to the end; with closes the file automatically.)

🧑‍🏫 Teacher's explanation

Imagine you are playing a video game. If you turn off the console without saving, your progress is gone. File handling is the 'Save Game' feature of computer science. It allows a program to store data on a permanent storage medium (like a hard drive) so that the data persists even after the program stops running.

📖 Definitions

🔍 Worked example — Easy

A program needs to store a single integer, 85, into a file named 'result.txt'. If the file already contains '50', what is the content of the file after using write mode ('w')?

🔍 Worked example — Standard

A program reads a file containing the text 'Hello\nWorld'. It uses a command to read only the first line. What is the output?

🔍 Worked example — Exam-style

A programmer wants to keep a log of temperatures. The file 'temp.txt' currently contains: \n15\n20\n. The programmer appends (mode 'a') the value 25. What is the final content of the file?

⚠️ Common errors

🧠 Quick check 1

🧠 Quick check 2

🎯 Exam question

State the mode that must be used for step 3. [1] [1 marks]

Worked example

Write pseudocode that asks the user for their name and saves it in a file called User.txt, then reads it back and outputs a greeting.

🔍 Worked example — Easy

Write pseudocode to create a file named 'scores.txt', write the integer 100 into it, and then close the file.

🔍 Worked example — Standard

A program needs to read a single line of text from a file named 'data.txt' and output it. Write the pseudocode.

🔍 Worked example — Exam-style

Trace the following pseudocode and determine the final output:

⚠️ Common errors

Common mistakes

Practice

Answers: (1) Data in an array is lost when the program ends; a file keeps it permanently for later use or sharing. (2) A new file is created and the existing data is lost. (3) OPENFILE "Notes.txt" FOR READ / READFILE "Notes.txt", LineOfText / OUTPUT LineOfText / CLOSEFILE "Notes.txt".

Summary

All chapters in Programming

  1. 8.1 Programming Concepts: Variables, Data Types, Selection and Iteration
  2. 8.1 Programming Concepts: Strings, Operators, Subroutines and Maintainable Code
  3. 8.2 Arrays
  4. 8.3 File Handling