Study Guide Instructor view

File Handling

Teaching notes for Lesson 5.15, with controls for presenting it.

Keep this page on your own screen. Share only the lesson window. The lesson window holds no teaching notes, so sharing it cannot show them.

Presenting

  1. Open the lesson in its own window and share that window, not this one.
  2. Use Next and Previous here, or the lesson’s own, to move through the lesson. Next reveals one thing at a time: the next idea, or the answer to the question on screen, then the next step. Previous takes the last reveal back. The line below says what is on screen and what Next will show.
  3. Reveal hints and answers when the class is ready, and reset a demonstration before using it again.

The controls need JavaScript and a browser that supports BroadcastChannel. Without them, open the lesson in presentation mode and use its own controls.

The two windows talk to each other only inside this browser, on this device. Nothing is sent to a server, and no account is needed. A second device (a phone or another tablet) cannot follow the lesson window yet.

Lesson plan

Level
Intermediate
Time
About 75 minutes; the steps below add up to 73

Pupils begin with a scoreboard that forgets its record and work out for themselves why variables are not enough and what a file must do. The three-stage pattern for writing and reading is then built from their predictions, the effect of WRITE mode on old data is predicted before it is explained, and pupils write and correct programs that use files.

Objectives

  • Explain why a program stores data in a file and why variables are not enough
  • Write pseudocode that opens a file for writing, writes single items and lines of text, and closes it
  • Write pseudocode that opens a file for reading, reads single items and lines of text into variables, and closes it
  • Say what happens to the data already in a file when it is opened for writing
  • Read a known number of lines from a file with a loop
  • Spot and correct mistakes in a program that uses a file

Before the lesson

  • A text editor and a small prepared text file (scores.txt) to show on the shared screen, if available; the lesson works without it.
  • Mini whiteboards for predictions about what the file holds.
  • Optional: a Python environment to run the Deeper example and show the file appear.

Steps

  1. Introduction Opening

    On screen: the lesson’s title, module and number; Next adds its objectives and key terms, as the lesson page lists them; Next again begins the first step.

    Objectives on screen

    • Explain why a program stores data in a file and why variables are not enough
    • Write pseudocode that opens a file for writing, writes single items and lines of text, and closes it
    • Write pseudocode that opens a file for reading, reads single items and lines of text into variables, and closes it
    • Say what happens to the data already in a file when it is opened for writing
    • Read a known number of lines from a file with a loop
    • Spot and correct mistakes in a program that uses a file

    Purpose: Pupils begin with a scoreboard that forgets its record and work out for themselves why variables are not enough and what a file must do. The three-stage pattern for writing and reading is then built from their predictions, the effect of WRITE mode on old data is predicted before it is explained, and pupils write and correct programs that use files.

  2. 1. A scoreboard that forgets Think 7 min

    Purpose: A scenario pupils know from games, leading them to the difference between memory that is lost and storage that is kept.

    Ask

    • Why did the app forget?
    • What would it need to remember tomorrow?

    Responses to expect

    • Variables are lost when the program ends
    • Somewhere that keeps data when it is switched off

    Misconceptions to challenge

    • A learner may believe: “The computer should remember everything it has done.” Respond: Main memory is cleared when the power goes or the program ends; only storage keeps data.

    Discussion pause: Thirty seconds, then share.

    In the lesson window

    • Think it through: The program stored the top score in a variable. Why had it forgotten the score by the next day? What would the program need so that it remembers between one run and the next?
  3. 2. Why programs use files Discover 8 min

    Purpose: The purposes of a file, introduced through pupils’ own examples.

    Ask

    • What else might a program need to keep or be given?
    • Why is a variable not enough?

    Responses to expect

    • Lists, attendance, settings, marks
    • It lives in volatile main memory

    Misconceptions to challenge

    • A learner may believe: “A variable is stored in a file.” Respond: A variable is held in main memory while the program runs.

    Checking understanding: The why-use-file question; each wrong option is a different wrong idea about variables.

    English as an additional language

    • Check volatile, storage and memory with a sketch of RAM and a disk.

    In the lesson window

    • Think it through: Apart from remembering a high score, give two other things a program might need to keep, or to be given, in a file. Think about a school: what data would you not want to type in again every time?
    • Check your understanding: A quiz program must still know the top score the next day. Why is a variable not enough?
  4. 3. Writing data to a file Predict 10 min

    Purpose: Pupils predict the three stages of writing before OPENFILE, WRITEFILE and CLOSEFILE are shown.

    Ask

    • What must the program do before it writes, and when it has finished?
    • What does the file hold afterwards?

    Responses to expect

    • Open, write, close
    • Two lines: Maya and 17

    Misconceptions to challenge

    • A learner may believe: “The file receives the names of the variables.” Respond: WRITEFILE puts in the values the variables hold.

    On the board or with the pen

    • Draw the file as a column of lines, adding Maya and 17 as the StepThrough advances.

    Checking understanding: The after-writing question.

    In the lesson window

    • Think it through: Before a program can put data into a file, what do you think it must tell the computer first? And what should it do when it has finished? Put the three stages in order.
    • Check your understanding: After the program has run, what does scores.txt contain?
  5. 4. Reading data from a file Discover 10 min

    Purpose: Reading as the mirror of writing, with the order of lines as the key idea.

    Ask

    • How does Maya get into Name?
    • What if What if the two READFILE lines were swapped?

    Responses to expect

    • READFILE takes the first line
    • Name gets 17 and Score gets Maya

    Misconceptions to challenge

    • A learner may believe: “READFILE looks for the variable name inside the file.” Respond: A file holds only lines. READFILE takes the next one, in order.

    Checking understanding: The read-trace table and the order-of-reading question.

    Support

    • Give the contents of the file as two cards, and let pupils pick up one card at a time as each READFILE runs.

    In the lesson window

    • Think it through: Using what you know about writing, what would the three stages of reading be, and how would the program get Maya into a variable called Name and 17 into a variable called Score?
    • Check your understanding: If the two READFILE lines were swapped, so that Score was read first, what would happen?
  6. 5. What writing does to old data Predict 9 min

    Purpose: Pupils predict that Maya’s line survives, then are corrected by the mode; the misconception is named at the point it arises.

    Ask

    • What does the file hold after Omar’s run?
    • How would you keep Maya’s result?

    Responses to expect

    • Omar and 20 only
    • Read it first, then write both

    Misconceptions to challenge

    • A learner may believe: “Writing adds to the end of the file.” Respond: In WRITE mode a file starts afresh and the old data is lost.
    • A learner may believe: “A file can be open for reading and writing at once.” Respond: The guide opens a file in only one mode at a time.

    Checking understanding: The second-run question.

    Depending on the answers

    • If many pupils expect the file to hold both names: run the writing program twice live (or on the board) and read the file between runs

    In the lesson window

    • Think it through: Predict what scores.txt holds when the program has finished. Does Maya’s line stay in the file? Give a reason.
    • Check your understanding: The writing program is run for Maya (17), and later for Omar (20), with no other changes. A reading program then runs READFILE into Name. What does Name hold?
  7. 6. Reading several lines with a loop Explain 12 min

    Purpose: A loop reads a known number of lines; the end-of-file test is offered only as an extension.

    Ask

    • Which loop reads five lines?
    • Where do the OPENFILE and CLOSEFILE go?

    Responses to expect

    • A FOR loop, because there are five
    • OPENFILE before the loop, CLOSEFILE after

    Misconceptions to challenge

    • A learner may believe: “The file should be opened inside the loop so it is fresh each time.” Respond: Opening again would start from the first line. Open once, read five times, close once.

    Checking understanding: The file-total trace and the close-in-loop question.

    Stretch

    • Read the file into an array first, then output the highest.

    In the lesson window

    • Think it through: Which kind of loop would you use to read the five lines, and why? Where would you put the line that sets the total to 0?
    • Check your understanding: Why is CLOSEFILE placed after the loop rather than inside it?
  8. 7. Write a program that uses a file Apply 10 min

    Purpose: Independent writing, then two fault-finding questions on modes.

    Ask

    • Write the program that saves three names.
    • What is wrong with writing to a file opened FOR READ?

    Responses to expect

    • OPENFILE FOR WRITE, a FOR loop, WRITEFILE, CLOSEFILE
    • The mode does not allow it

    Misconceptions to challenge

    • A learner may believe: “The mode does not matter if the file name is right.” Respond: The mode decides what may be done, so it must match the task.

    Checking understanding: Collect the paper programs; check open before, close after and the loop.

    Support

    • Give the OPENFILE and CLOSEFILE lines and ask for the loop in between.

    In the lesson window

    • Think it through: Write pseudocode that asks for the names of three players, one at a time, and writes each name on its own line in a file called team.txt.
    • Apply it: What is wrong with the three lines of this program?
    • Apply it: What is the result of running these two lines on a file that holds a saved score?
  9. 8. Show what you know Consolidate 7 min

    Purpose: Explanation of purpose and the three stages, and a decision about keeping old data.

    Ask

    • Why does the app need a file?
    • How would a teacher add a result without losing the old ones?

    Responses to expect

    • Variables are lost; files are kept
    • Read the old results, close, write old and new

    Checking understanding: The keep-old-results question shows who has taken in the effect of WRITE mode.

    In the lesson window

    • Think it through: Explain to a friend why the quiz app needs a file to remember the top score, and describe the three stages and the commands for each when the app saves a score. Say what would happen to the old score.
    • Apply it: A teacher’s program must add a new pupil’s result to a file that already holds earlier results, without losing them. Which approach is correct using the commands in this lesson?
  10. Lesson recap Closing

    On screen: the lesson’s objectives again; Next adds the closing question, which asks the class to explain each one in their own words without looking back. It says nothing about what anyone has learnt: use the answers to decide what to revisit.

    Points to listen for

    • A file keeps data on secondary storage after the program ends; a variable does not.
    • The three stages are OPENFILE with a mode, READFILE or WRITEFILE, and CLOSEFILE.
    • Opening a file FOR WRITE replaces what it held.
    • A known number of lines is read with a loop; the file is opened before it and closed after it.

Consolidation

  • A file keeps data on secondary storage after the program ends; a variable does not.
  • The three stages are OPENFILE with a mode, READFILE or WRITEFILE, and CLOSEFILE.
  • Opening a file FOR WRITE replaces what it held.
  • A known number of lines is read with a loop; the file is opened before it and closed after it.