CS 150 - Intro to Programming - Fall 2001
Lab Exercise 9

Topics:

Part 1 - File Stream Input and Output

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.

  1. Create an input file containing 15 integers (Click the New Text File Icon on the CodeWarrior toolbar), then enter 15 integers separated by a combination of spaces, tabs, and carriage returns. Make sure and use a variety of integers (such as 3, 450, 9, 75534, etc) and make sure the file ends with a whitespace. Save the input file using the name "lab9Part1.dat" (make sure to save it in your project folder).

  2. First read the integers from the file using the extraction operator ">>" and an integer variable. Recall that the extraction operator skips whitespace. Create a program (yourLastNameLab9Part1b) that reads the integers from the file one-by-one, and displays them to the screen one-per-line. Use the form: inputStream >> integerVar to read the data. The program should stop when end-of-file has been reached. (ex. while(!inputStream.eof()))

  3. Next modify the program (save as yourLastNameLab9Part1c) so that it reads the data in the file as characters, again using the extraction operator. Use the form: 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?

  4. Next modify the program (save as yourLastNameLab9Part1d) so that it reads in the characters using the input stream member function 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.

  5. Finally modify the program from part b (save as yourLastNameLab9Part1e) to write the output to a file ("lab9Part1Output.dat") as well as to the screen. Print each integer in a field 12 characters wide with 5 integers per line. Verify that the output file was created by opening it with CodeWarrior or notepad (or simpleText if you are working on a Mac).


Part 2 - End of file issues

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)


Part 3 - More File I/O

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


Part 4 - File streams as function arguments

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").


Part 5 - Using multiple files in a single program

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.

What to turn in

Remember to: