Chapter 13: Programing and Data Representation

Nov 24, 2019

Overview of the chapter:

  • 13.9 Text files

13.9 Handling text files

Some data needs to be stored permanently. If stored in variables data will be lost when program stops / powered off. If stored in a file the data is no longer volatile.

You need to be able to perform the following tasks:

  • Open the file, in read / write / append mode
  • Operate the file, read / write
  • Close the file

In the following languages:

  • Python
  • Pseudocode

13.9.1 Opening files

There are three methods of opening files: Read, Write, Append.

ModeExplanationPseudocode syntaxPython Syntax
ReadOpen the file for reading its contents.OPENFILE <filename> FOR READfile = open(“<filename>”, “r”)
WriteOpen the file for writing new contents.
By writing all original data will be overridden.
OPENFILE <filename> FOR WRITEfile = open(“<filename>”, “w”)
AppendAdd more data to the end of the file.OPENFILE <filename> FOR APPENDfile = open(“<filename>”, “a”)

Note that:

  • In pseudocode there is no need to covert filename to string. But in python the filename must be in a string.
  • In pseudocode the modes are key words, while in python the mode are strings.

It is not recommended to use the method of “WITH OPEN”. The method doesn’t need to close the file explicitly, but there is a mark given for the command. Teachers will often miss that mark if you use “WITH OPEN”.

13.9.2 File operations

Read the next line from fileREADFILE <filename>, <variableToAssign><variableToAssign> = file.readline()
Write a line to fileWRITEFILE <filename>, <stringValue>file.write(<stringValue>)

The READLINE command reads the next line of the file.

If the file is opened in “READ” mode, the WRITEFILE command will first erase everything in the file, then write the stringValue in.

If the file is opened in “APPEND” mode, the WRITEFILE command will only add the stringValue to the end.

13.9.3 Closing the file

Always close the file after editing. This command will update all the changes to the file.

Close the fileCLOSEFILE <filename>file.close()

13.9.4 End-of-file marker

The marker is the only way to identify the whole file had been finished reading.

Find if the file have been read completelyEOF( )
// The function returns TRUE
//if the end of file had been reached.
data = file.readline( )
if len(data) == 0:
//End of file if the line read is empty.

Sample code:


DECLARE total, grade: INTEGER <- 0

WHILE NOT EOF("Grades.txt") DO     //Yes, the parameter is a STRING!
    READFILE Grades.txt, grade
    total <- total + grade

CLOSEFILE Grades.txt
total, grade = 0, 0

file = open("grades.txt", "r")
grade = "-"

while len(grade) != 0:
    grade = file.readline()
    if len(grade) != 0:      #Yes, I want to use break too...
        total += int(grade)

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.