Topics:
In this part of the lab we will investigate the different ways to read data from a file and how to write formatted data to an output file. If you haven't already read Chapter 5 of Savitch, you will find it helpful to do so before attempting this exercise. You will also want to refer to your notes and code samples from the relevant lecture.
inputStream
>> integerVar
to read the data. The program should stop when end-of-file has been reached. (ex. while(!inputStream.eof())
)
inputStream >> charVar
. The program should
stop when end-of-file has been reached. What is different about the output? Are any characters in the original
file missing?
get(charVar)
. Recall that the get member function does not skip any whitespace.
Use the form: inputStream.get(charVar)
.The program should stop when end-of-file has been reached.
In class we discussed different ways of reading from a file - it turns out that there is a subtle difference in the way the EOF marker gets read when reading numeric data from the a file. See the code samples below. In sample a the loop is terminated when inData fails to read an integer from the file stream or encounters the EOF marker. In sample b the loop is terminated when the end of file marker has been set. Both ways work fine as long at there is some white space after the last integer in the file - if not, the second method will fail to print the last integer as the end of file marker gets read with the last integer.
a.) int num; ifstream inData("someFile"); while(inData >> num) { cout << num << endl; } |
b.) int num; ifstream inData("someFile"); inData >> num; while(!inData.eof()) { cout << num << endl; inData >> num; } |
a. Modify the file "lab9Part1.dat" so that there is only 8 integers in the file, and no white space
after the last integer. Save this file as "lab9Part2.dat"
b. Write a C++ program (yourLastNameLab9Part2b) that reads the integers using method a above. Run the program
and note the results.
c. Write a C++ program (yourLastNameLab9Part2c) that reads the integers using method b above. Run the program
and note the results. What happened to the last integer??? - Which method should you use when reading from a file???
- Put your comments in the code for this part (yourLastNameLab9Part2c)
The formula for determining the power consumption of a device is P = I * E, where I is the current in amps and E the voltage in volts. Copy and Paste the data below into a file named "lab9Part3.dat". Note: the file contains two columns of floating point numbers - the first column represents the current readings and the second column contains the voltage readings (note there is no special formatting in the file).
a. Write a C++ program (yourLastNameLab9Part3a) that reads the data from the file, calculates the power, and writes the current, voltage and power to a file ("lab9Part3aOutput.dat"). The file should contain the headings current - voltage - power and data entries one per line. (Set field widths to 14 for output). Data should be output using four decimal place precision. Use stepwise refinement to solve this problem - first read the data and echo to screen - when working just change the output stream to the file. This will save you from having to open the output file after each test making the program easier to test.
//input data - lab9Part3.dat
0.0034 12.54 132.7 12024 10.3 120.72 10.6 172.72 55.68 76.32 0.000666 3.125
//sample output file - lab9Part3aOutput.dat
current voltage power 0.0034 12.5400 0.0426 132.7000 12024.0000 1595584.8000 10.3000 120.7200 1243.4160 10.6000 172.7200 1830.8320 55.6800 76.3200 4249.4976 0.0007 3.1250 0.0021
a. Convert the program from part three (save as yourLastNameLab9Part4a) to use functions to open the files and set the precision. Pass the input and output file streams as reference parameters to the file opening function - you will need two parameters - one for the input stream and one for the output stream. The function should open the files - print a message if a failure occurred, and return a true or false value to indicate success or failure. (see the fileAvg codeSamples for an example). Save the output to file ("lab9Part4aOutput.dat").
a. Programmer-defined functions can be defined in the same file as the main function or in a separate file so that the functions can be used by several different programs. The next few steps will change the program you wrote in lab7Part2 so that it consists of three separate files; one containing the function main (somethingMain.cpp), one containing the declaration of the function prototypes (interface file - something.h) and one containing the function implementations (implementation file - someFunctions.cpp). See moreSort3 and moreSortAll in the codeSamples for an example of this process.
#include "yourLastNameLab9Part5Functions.h"
Note the
use of the quote (") symbol in place of the (< and >)