YOU HAVE A TASK TO DO!!!!!!!!

There has been a new version of macStuff.cpp created. You must go and get it from PUBLIC. It is in: CS5 --> CS Software Parts. In here there are two subdirectories: CS Headers and CS Sources. Copy macStuff.h from CS Headers and macStuff.cpp from CS Sources and put them into Metrowerks Code Warrior --> CS Headers and Metrowerks Code Warrior --> CS Sources respectively on your own computer!!

Streams, Files, and Objects

Unbeknowst to most, if not all of you in the class, you have been using streams for input from and output to the console. Graphically what has been happening is your programs have been taking input from the stream cin, doing some work on this input, and then writing the changes to the stream cout.

And you have been making use of these streams by using the operators on the streams:

In fact, these are overloaded operators. This means that when invoked they behave differently for ints than for streams. As can be seen in the demos throughout the lecture there are some neat things that we can do with streams. The first thing is that it is easy to copy cin to cout.

See Demo echo.cpp

Some things to note about the echo.cpp Demo

More About Streams

A stream is an example of an object. Objects contain both member data and member functions. As we have seen above streams have a member function unsetf . So cin.unsetf( ... ) executes the member function as it is defined for the input stream cin.

Files

There is nothing all that different between reading from the console and reading from a file on the disk. Graphically they look similar:

Or writing to the console and writing to a file:

The big advantage is that we can use the same >>, << operators for these operations. However, we must be careful because file objects are a little more complicated than the console I/O objects that we have been used to dealing with since day one.

What needs to be done to file objects??

See Demo echoToFile.cpp

Some interesting things about echoToFile.cpp:

Here is another demo...

This is a demo that reads from a file to the console...

See Demo echoFromFile.cpp

Things to note

The following demo concatenates two input files to one output file.

See Demo cat.cpp

Things we did not get to in class ...

There are other demos that I did not get the chance to get to in class. They involve the formatting of data and I strongly suggest that you go run them and examine the code to get at feel for what is going on in them. See Demo format.cpp and max.cpp.

To Index Previous Next