EEasy IGCSELabTracker

On a phone the notes, quizzes and tracker work fine. The 3D models and the practice lab are made for a laptop.

Specification points 8.3.1–8.3.2 ·

why programs store data in files; opening, reading, writing and closing files for single items of data and lines of text.

1Why store data in a file

Program running variables and arrays in RAM Data is lost when the program closes or power is off File on storage e.g. "Scores.txt" Data is kept permanently after the program closes WRITEFILE READFILE
Figure 1. Variables only exist while the program runs; a file keeps the data.

Open, write, close, read

Loading the interactive…

Storing data in a file keeps it permanently, so it does not have to be entered again each time the program runs (e.g. a saved high score or a list of names). The file can be used later by the same program, by another program, or on another computer.

2Opening, using and closing a file

Step Pseudocode What it does
Open to read OPENFILE "Scores.txt" FOR READ opens the file so data can be read from it
Open to write OPENFILE "Scores.txt" FOR WRITE creates a new file for writing — any data already in it is lost
Read READFILE "Scores.txt", Score reads the next data item or line from the file into the variable
Write WRITEFILE "Scores.txt", Score writes the value of the variable into the file
Close CLOSEFILE "Scores.txt" closes the file when it is no longer needed
  • Always open a file, stating the mode, before using it, and close it afterwards.
  • A file is open in one mode at a time. To read back data you have just written, close the file and open it again FOR READ.
  • The file identifier is the file name, a string. Each READFILE reads the next item or line in order; a line of text is read into a STRING variable.

3Reading and writing single items and lines of text

A single item of data — save a high score and load it next time:

OPENFILE "HighScore.txt" FOR WRITE
WRITEFILE "HighScore.txt", HighScore
CLOSEFILE "HighScore.txt"

OPENFILE "HighScore.txt" FOR READ
READFILE "HighScore.txt", HighScore
CLOSEFILE "HighScore.txt"

Lines of text — write 30 names from an array, one per line, then read them back:

DECLARE LineOfText : STRING
OPENFILE "Names.txt" FOR WRITE
FOR Index ← 1 TO 30
    WRITEFILE "Names.txt", Names[Index]
NEXT Index
CLOSEFILE "Names.txt"

OPENFILE "Names.txt" FOR READ
FOR Index ← 1 TO 30
    READFILE "Names.txt", LineOfText
    OUTPUT LineOfText
NEXT Index
CLOSEFILE "Names.txt"

Two files can be open at once in different modes — e.g. copy a line from one file to another: open "FileA.txt" FOR READ and "FileB.txt" FOR WRITE, READFILE from A into a variable, WRITEFILE that variable to B, then close both.

Traps
  • Opening an existing file FOR WRITE erases its contents.
  • READFILE and WRITEFILE name the file, then the variable: READFILE "Names.txt", Name.
  • Do not read from a file opened FOR WRITE (or write to one opened FOR READ).

Quick check

10 multiple-choice questions. Pick an answer to see whether it is right and why.

  1. Why does a game store its high score in a file?

  2. Log.txt contains 5 lines. A program runs OPENFILE "Log.txt" FOR WRITE, then WRITEFILE "Log.txt", Entry once, then CLOSEFILE "Log.txt". How many lines does the file now contain?

  3. Which statement reads the next line of Names.txt into the variable Name?

  4. Towns.txt contains the lines Lima, Oslo and Cairo, in that order. It is opened FOR READ and READFILE "Towns.txt", Town is executed twice. What is the value of Town?

  5. A program has just written data to Data.txt and now needs to read it back. What must it do first?

  6. A program runs OPENFILE "Pts.txt" FOR READ, then WRITEFILE "Pts.txt", Points, then CLOSEFILE "Pts.txt". What is the error?

  7. Which data type should the variable have when READFILE reads a line of text into it?

  8. Nums.txt holds 4, 10, 6 and 3, one per line, and is opened FOR READ. Total ← 0 is followed by a loop FOR Index ← 1 TO 3 containing READFILE "Nums.txt", Value then Total ← Total + Value. What is Total after the loop?

  9. A program copies a line from FileA.txt to FileB.txt. How should the two files be opened?

  10. Which order of statements saves the value of Score in Best.txt?

Exam-style questions

Write your answer, then show the answer and compare. Nothing here is marked for you: you decide how close you were. What you write is saved in this browser.

Recall and understanding

1

Explain why a program stores data in a file.

2 marks
2

State what happens to the data already in a file when it is opened FOR WRITE.

1 mark
3

State why a file should be closed before it is opened again in a different mode.

1 mark

Errors

4

Identify three errors in this algorithm, which should read a score from Data.txt.

OPENFILE "Data.txt" FOR READ
WRITEFILE "Data.txt", Score
READFILE Score, "Data.txt"
3 marks

Writing pseudocode

5

Write an algorithm to store the value of Username in the file users.txt.

3 marks
6

Write an algorithm to read the first line of story.txt and output it with its length.

4 marks
7

The array Scores[1:10] holds 10 integers. Write an algorithm that saves them to scores.txt, then reads them back from the file and outputs their total.

6 marks

How well do you know this?

Rate each specification point. It is saved to your tracker as a new revision round.

All points:
  • 8.3.1Understand the purpose of storing data in a file to be used by a program
  • 8.3.2Open, close and use a file for reading and writing

Settings

Your subjects

Untick the subjects you don't take. They leave the menu, the home page and the tracker. Your ratings are kept, so you can bring a subject back at any time.

Page style

Colours