files

data is stored even after we close the program!

Files are awesome! They allow us to store data, kind of like a database.

Computer programs can open files and read them and write to them.

When writing to a file, we can overwrite, or we can append.

Here is an example of a file.

As you can see, each line of the file has the first name of a student, followed by a score, followed by a letter grade.

In PSEUDOCODE, we have a variety of keywords to help programmers understand what we want them to do when handling files:

OPENFILE filename FOR READ/WRITE/APPEND

READFILE filename, line

WRITEFILE filename, line

Also, when we are reading a file, this code is soooooooo useful:

OPENFILE filename FOR READ

WHILE NOT EOF(filename) DO

//file processing

END WHILE

CLOSEFILE filename

And, as the above pseudocode illustrates, you need to know when to close your file:

CLOSEFILE filename

Task 1 - Sample Exam Style Question #1

Write a subroutine which takes a filename as a parameter.

The subroutine opens the file and will ask the user to enter:

  • firstname
  • score
  • grade

and append the data into the file in the format shown in the above sample.

This process will continue until the user enters "0" as the first name.

The subroutine will return the number of new lines of information that were appended to the file.

A sample computer program might be:

DECLARE newLines : INTEGER

newLines ← addScoreData("studentScores.txt")

OUTPUT newLines," records of data were added."

Task 2 - Sample Exam Style Question #2

Using the above file as a sample, we are going process the file.

A new grade, A*, is to be included in the range of grades available.

Write a subroutine which will take a filename and an integer as parameters.

The subroutine will count how many students in the file achieved a grade greater than or equal to the integer parameter.

This count is returned by the subroutine.

A sample computer program might be:

DECLARE totalAStars : INTEGER

totalAStars ← countAStars("studentScores.txt", 92)

OUTPUT "There are ", totalAStars, "A Star students in the file."

Implement this subroutine in pseudocode.


Study Strategy

Don't be too distracted by exactly precise pseudocode as you start to answer the question.

However, don't forget to read over your work later and correct any little inaccuracies if you can.


Tags

stay calm think try sleep dream fresh strong spirit