Objectives

Students will be able to:

  • show understanding of while files are needed
  • use pseudocode for file handling:
  • OPENFILE <filename> FOR READ/WRITE/APPEND
  • READFILE <filename>, <string>
  • WRITEFILE <filename>, <string>
  • CLOSEFILE <filename>
  • EOF <filename>
  • write program code for filehandling of a textfile
Files

Files

great for storing data


Files

A computer program uses a data structure, for example an array, to store data. The problem is, when the computer is turned off, all of the data is lost.

One way to fix this problem is to store the data in a text file. ↓ is an example of a simple text file.

The data can be stored in the file and then read into the computer program line by line, possibly into an array, for example, for processing.

The computer program can also write new data into the text file. When the textfile is closed, all of the information will be available the next time it is opened.

One very interesting thing to note is that a text file stores text ie STRINGS. It does not store numbers as INTEGERS or REALS ie it stores "216" not 216.

Let's have a go. We are going to write a computer program that will:

    open a text file
    get some data from a user
    store the data in the file

Pseudocode

OPENFILE "myText.txt" FOR WRITE

OUTPUT "Enter name, age and gender"

INPUT name

INPUT age

INPUT gender

WRITEFILE "myText.txt", name

WRITEFILE "myText.txt", age

WRITEFILE "myText.txt", gender

CLOSEFILE "myText.txt"

   Task 1

A computer programmer has been trying to develop the above algorithm in Python. It doesn't work! Perform some corrective maintenance on the code.

We can also read a text file.

For example a textfile stores names and ages of adults in the following format:

"James McIntyre_23"

"Ellie May_26"

An algorithm has been designed to calculate the average age of each person in the file.

In pseudocode, it uses a built-in function called RIGHT(s, n) which returns n characters from the right side of String s.

Pseudocode

PROCEDURE displayTotalAge(filename:STRING)

DECLARE totalAge : INTEGER, age: INTEGER, line : STRING

totalAge ← 0

OPENFILE filename FOR READ

WHILE NOT EOF(filename) DO

#read a line from the file and store it in a variable called line

READFILE filename, line

#get the final 2 characters of the line and convert to an integer.

age = INT(RIGHT(line, 2))

totalAge = totalAge + age

END WHILE

OUTPUT "The total age is ", totalAge

#don't forget to close the file

CLOSEFILE "names.txt"

END PROCEDURE

Tricky stuff

When we express that we are reading a file from beginning to end in PSEUDOCODE we do this:

WHILE NOT EOF(filename) DO

How would we do this in Python or Java or C or Ruby or PHP or...? During this unit you will research the answer to this in Python.

   Task 2

Using Python, code a solution to the above procedure, displayTotalAge.

You can use the file at the very top of this unit page see the brown button to help you!

   Summary of modes

mode description
r opens the file in read-only mode. File pointer is at the start of the file.
r+ opens a file for both reading and writing
w opens a file for writing only. Overwrites the existing file and creates a new file if it does not exist.
w+ opens a file for both reading and writing. Overwrites the existing file and creates a new file if it does not exist.
a opens a file for appending. Data is appended at the end of the file. Creates a new file if it does not exist.
a+ opens a file for both appending and reading. File pointer is at the end of the file. Creates a new files if it does not exist.
Inspired by 101 Computing

Feelings

How do you feel right now?