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:
- >> To get something from cin, e.g.,
- << To insert something into cout, e.g.,
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
- cin.unsetf(ios::skipws);
- Tells cin to "unset" its "flag" for skipping white
spaces which includes blanks, tabs, newline character.
- Flag - Another name for a logical value.
- Unset - Make a flag be false.
- Set - Make a flag be true.
- So cin.unsetf(ios::skipws) says to read all
characters.
- while ( cin >> c )
- Can use "read from stream" expresion as a
condition (or logical value):
- 0 if nothing more to read.
- Not 0 if something was read.
- We indicate nothing more to read in the console input by
END key or control-D.
- There is an extra cout << endl; at the start
which avoids a new CodeWarrior glitch.
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??
- We need to establish a correspondence between a file
on Mac and the stream object in your program. This is automatically done
for the streams cin and cout, which is why we did not have to worry about
these things until now.
- We need to open and close the file.
- We need to check that the file opened successfully.
- And mother always said, "If you open it, you shut it!!" So we will be
good prgrammers and close the files we do not need after we have used
them.
See Demo echoToFile.cpp
Some interesting things about echoToFile.cpp:
- ofstream outFile;
- Declares outFile to be like cout but an output file stream.
- outFile.open( ... );
- Opens the output file for writing to.
- newFileName( ... );
- Special Mac-only function we graciously provide in macStuff.cpp to get
a file name.
- The new version which you can get by following the directions above has
overloaded newFileName( ... ) that takes a prompt string. This allows
for creative prompts in the dialog box when creating a new file.
- The book talks about file names and how to get them. Disregard the
book's description of file names. This is only for people in the PC
world, and as you all well know, we have the, ahem, advantage of
using Macs.
- What is the return type of newFileName? We will see later. For now
just use it as an actual paramter of open.
- If file exists, it will ask you if you want to overwrite it.
- openConsole();
- Must call before opening dialog box!!!
- if ( outFile.fail() )
- Checks if open did not work.
- You should do this for files before you try to read or write to them.
- However, you do not need to check for cin, cout, or cerr.
- cerr << ...
- cerr is a special error stream.
- It automatically gets opened like cin and cout do.
- Like cout it is also mapped to the console.
- It is different from cout though. For instance, on a real computer
we might suppress cout but we still want the
functionality to write to the console. Hence we would use cerr.
- outFile << c;
- outFile.close();
- Close out file when we are done because mama said so!!!
Here is another demo...
This is a demo that reads from a file to the console...
See Demo echoFromFile.cpp
Things to note
- oldFileName();
- Like newFileName() but requires the file to already exist.
- inFile.unsetf(ios::skipws);
- Like cin.unsetf(ios::skipws).
- while ( cin >> c )
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