Professional Documents
Culture Documents
Name: ___________________________
Due: ___________________________
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:
A data file must also be created to store the output, such as:
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.
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:
End Module
3
End Module
Critical Review
When writing to a text file, an internal file name must be created such as outFile.
ofstream outFile;
Files must then be closed. This works the same for both input and output.
outFile.close(); or inFile.close();
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.
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.