CS253

This file defines the header for each page. An optional "icon" image (I use the textbook):

Replace this with info about this class:

CS253: Problem Solving with C++

Spring 2014

IO Lab

Links to the various pages for this class:

Wish I could do this: * Schedule

I/O Lab

Description

In this lab, we’ll do some exercises with I/O streams.

The files for this lab are available in ~cs253/pub/Labs/IOLab.

Your Lab assignment is to modify the number.cc class and modify.cc class so that it fulfills the requested requirements at the end of each part.

Robust input operators

Consider number.cc. It creates a class called Number that’s a wrapper around an int.

  1. How is it that << works for Number, even though operator<< wasn’t defined for this class?
  2. Consider operator>>. It doesn’t check for failure. And, yet, it works properly for:
    • valid numeric input
    • end of input
    • incorrect input
    How can this be?
  3. Now, improve operator>> so that it works properly for input such as “one”, “two”, “three”, “four”, and “five”, as well as working for all traditional numeric input that worked before.
  4. Show me your result at this stage.

Using fstream to modify a file in place.

Consider modify.cc. It copies its input file to its output file, replacing all instances of “Bush” with “Gore”.

Note the use of getline. There are two versions of this function:

Change modify.cc to modify a single file in place. That is, the program should only take a single filename argument.

Hints:

  1. If you read the entire file into one giant string, you’re doing it wrong. Your code should work for colossal files that don't fit into memory.
  2. What sort of open mode should you use?
  3. This approach only works if the replacement string is the same length as the original string. However, it sure is efficient.
  4. Also show me the result at this stage.

For extra fame & glory

Improve operator>> in number.cc even more:
If it encounters invalid input such as “zork”, it shouldn’t consume the bogus input. It should fail, and leave things such that the next read (assuming that the stream is put back into a good state) would see “zork” again.

Modified: 2014-03-29T19:20

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building