Assignment - File Access and Pseudocode
Critical Review
When a program needs to save data for later use, it writes the data in a file. The data can be read from the file at a later time.
Three things must happen in order to work with a file. 1) Open a file. 2) Process the file. 3) Close the file.
An internal file must be created for an output file or input file, such as:
Declare OutputFile myFile //to write out
Declare InputFile myFile //to read in
A data file must also be created to store the output, such as:
Open myFile "thedata.txt"
New keywords and syntax include the following:
Open [InternalName] [FileName]
Write [InternalName] [String or Data]
Read [InternalName] [Data]
Close [InternalName]
AppendMode //used with Open when need to append
Loops are used to process the data in a file. For example:
For counter = 1 to 5
Display "Enter a number:"
Input number
Write myFile number
End For
When reading information from a file and it is unknown how many items there are, use the eof function. For example:
While NOT eof(myFile)
Read myFile number
Display number
End While
This lab examines how to work with a file by writing pseudocode. Read the following programming problem prior to completing the lab. The following program from Lab 9.1 will be used, with some modifications.
The American Red Cross wants you to write a program that will calculate the average pints of blood donated during a blood drive. The program should take in the number of pints donated during the drive, based on a seven hour drive period. The average pints donated during that period should be calculated and written to a file. Write a loop around the program to run multiple times. The data should be appended to the file to keep track of multiple days. If the user wants to print data from the file, read it in and then display it. Store the pints per hour and the average pints donated in a file called blood.txt.