That Blue Square Thing

AQA Computer Science GCSE

July 2019: the AQA CompSci area is almost complete. Most of the content for units 3-6 is now up and there's quite a lot of Unit 7 material. There's still some work to do, particularly on Paper 1 units, but it's getting there.

Programming Concepts - Advanced File Handling

The read/write program developed on the read/write files page is one which can easily be decomposed using subroutines.

Decomposing programs make them easier to code, simpler to test and means that sections of the program can be re-used in other programs. These are all major advantages.

Make sure you have a copy of the text file highscores.txt saved to use with this program. Remember, the text file needs to be saved in the same folder as your program.

Text file iconHighscores text file - right click and Save As

Adding Subroutines

Two subroutines can be used here: one to read a file in and one to write data to a file.

The abstracted pseudocode for this problem is:

SUBROUTINE readFile(filename)
# read the file in
# return the data to the main program

SUBROUTINE writeFile(filename, dataToWrite)
# write the data to the file

# main program
newscore = "Wilbur Weasel, 150"

scores <— readFile("highscores.txt") # call subroutine
scores <— scores + newscore
writeFile("highscores.txt", scores)

newScores <— readFile("highscores.txt")
OUTPUT newScores

Each subroutine can be written as a general subroutine by passing the file name to it. This means that I can now re-use each of the subroutines in any program that I write that needs files to read or written.

Because they are re-usable in any program, general subroutines are really useful to write.

The Read Subroutine

The readFile subroutine would be written as follows:

# function to read in a text file
def readFile(filename):
theFile = open(filename, "r")
theData = theFile.read()
theFile.close()

return theData # returns the data to the main program

When the subroutine is called from the main program the name of the file is passed to the subroutine as a parameter. Within the subroutine it is then used by using the local variable name filename. When the data is returned to the main program it is stored in the global variable called scores.

This would be done by using this code in the main program:

scores = readFile("highscores.txt")

Note then within the general subroutine I've used very general variable names. If this subroutine is going to be reused in a number of programs, I don't know if it will be read in a text files of highscores, usernames, passwords or something else.

The Write Subroutine

The writeFile subroutine takes two parameters because we need to pass the data to be saved in the text file to the subroutine as well as the name of the textfile.

The subroutine doesn't return a value to the program - it just writes the data to the textfile instead.

# function to write to a text file
def writeFile(filename, dataToWrite):
theFile = open(filename, "w")
theFile.write(dataToWrite)
theFile.close()

The subroutine would be called from the main program with the code:

writeFile("highscores.txt", scores)

For even more advanced file handling, using lists (arrays) you can go to the Using Readline page and the Read/Write Lists page in the Programming Project unit.