You are on page 1of 5

1

Lab 10: File Access


This lab accompanies Chapters 10 of Starting Out with Programming Logic & Design.

Name: ___________________________

Due: ___________________________

Lab 10.1 – File Access and Pseudocode

Critical Review

When a program needs to save data for later use, it writes the data to 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 outFile // to write out


Declare InputFile inFile // to read in

A data file must also be created to store the output, such as:

Open outFile “thedata.txt”

New operations include the following:

Open InternalName “FileName”


Write InternalName String or Data
Read InternalName Data
Close InternalName

Loops are used to process the data in a file. For example:

For counter = 1 to 5
Display “Enter a number:”
Input number
Write outFile number
End For

When reading information from a file and it is unknown how many items there are, use the
eof function. It is similar to a sentinel loop. For example:
Read inFile number
While NOT eof(inFile)
Display number
Read inFile number
End While
2

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 8
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, up to twenty-four-hour drive
period, from an input file, bloodDrive.txt. The average
pints, highest pints, and lowest pints donated during that
period should be calculated and written to a file called
bloodResults.txt. We will only run the program once so a
yes/no loop is not needed.

Step 1: Note that the getHigh, getLow, and getAverage functions do not change. We
need to modify modules getPints and displayInfo to meet the new requirements.
Complete the pseudocode below for the two modules according to instructions:

In the getPints Module (notice there is a new pass-by-reference parameter count)


a. Declare an input file with the name bloodInFile.
b. Open the internal file (bloodInFile) and a text file named
bloodDrive.txt.
c. If cannot open file then display an error message
d. Set count = 0
e. Input one value from the file
f. While not end-of-file, store value in the array[count], add 1 to count, and input
another value
g. Close the file.

Module getPints(Real pints[], Integer Ref count)

End Module
3

In the displayInfo Module


a. Declare an output file with the name bloodOutFile.
b. Open the internal file (bloodOutFile) and a text file named
bloodResults.txt.
c. Write the string “Report generated by [YourName] ” to the file.
d. Write the string “Blood drive period: ” to the file.
e. Write the value of hours to the file.
f. Write the string “Average Pints: ” to the file.
g. Write the value of averagePints to the file.
h. Do the same thing for highest pints and lowest pints
i. Close the bloodOutFile.

Module displayInfo(Integer hours, Real averagePints,


Real highPints, Real lowPints,)

End Module

Pseudocode for the main module (MAX_HOURS is a global named constant)


Module main()
//Declare local variables
Declare Real pints[MAX_HOURS]
Declare Integer hours
Declare Real avePints, highPints, lowPints

Call getPints(pints, hours)


Display “hours: “, hours
Display “first value: “, hours[0]
Display “last value: “, hours[hours – 1]
Set avePints = getAverage(pints, hours)
Set highPints = getHigh(pints, hours)
Set lowPints = getLow(pints, hours)
Call displayInfo(hours, avePints, highPints, lowPints)
End Module
4

Lab 8.2 – File Access and C++ Code

Critical Review

Working with files

You need to include <fstream> in order to work with files in C++.

Writing to a text file

When writing to a text file, an internal file name must be created such as outFile.
ofstream outFile;

This file must then be opened for output. For example:


outFile.open(“filename.txt”);

Values can be written to a file, such as:


outFile << “a: ” << a << endl;

Files must then be closed. This works the same for both input and output.
outFile.close(); or inFile.close();

Reading from a text file

When reading from a file, an internal file name must be created such as inFile.
ifstream inFile;

This file must then be opened for input. The external file must exist or an error would occur. For
example:
inFile.open(“filename.txt”);

Reading from a file is done sequentially in this lab and once a value is read that variable can then
be used for processing within the program.

A value can be read from a file and displayed to the screen, such as:
inFile >> a;
cout << “a: “ << a << endl;

A loop and end-of-file (eof) can be used to input data from a file with an unknown
number of values to an array (array variable arr holds a list of values and variable
count hold number of values.
count = 0;
inFile >> a;
while (!inFile.eof())
{
arr[count] = a;
count++;
inFile >> a;
}
5

The goal of this lab is to convert the blood drive program from Lab 10.1 to C++ code.
Create a text file name bloodDrive.txt in the project folder with the following values (or
you can also get it from Canvas). Make sure to put one blank line after the last value.
43
25
64
35
19
37
46

Run your program and check against the following output file, bloodResults.txt. If there
are errors, go back through the steps to troubleshoot. You should confirm that the
program displays 7, 43, and 46 to the screen for hours, first value, and last value.

Report generated by [YourName]


Blood drive period: 7
Average pints: 38.4
Highest pints: 64.0
Lowest pints: 19.0

You might want to try another test case with fewer or more than 7 values to make sure
your program can handle it. Don’t forget to include <fstream> for file input and output.
You would need to include <iomanip> and set the precision for the output file (not
cout) to get one digit after the decimal point.  When your code is complete and runs
properly, copy/paste both the source code and the output file (bloodResults.txt) for the
test case above. 

Describe your experience and lessons learned with this lab. 

Lab 10.3 – Extra Credit


Modify the program in previous step so it also outputs the hours when highest pints and
lowest pints were collected (can submit one final version instead of two versions). We
will label the first hour as 1 instead of 0. The output file for the same test case would be:

Report generated by [YourName]


Blood drive period: 7
Average pints: 38.4
Highest pints: 64.0 (hour 3)
Lowest pints: 19.0 (hour 5)

You might also like