\documentstyle[12pt,html]{article}
\oddsidemargin 0.25in
\topmargin -0.5in
\textheight 8.5in
\textwidth 6.25in
\newcommand{\realspace}{
   \renewcommand{\baselinestretch}{1.5}
   \large\normalsize}
\newcommand{\singlespace}{
   \renewcommand{\baselinestretch}{1.0}
   \large\normalsize}
\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\newcommand{\bv}{\begin{verbatim}}
\newcommand{\edv}{\end{verbatim}}

\input epsf
\begin{document}
\setcounter{page}{141}

\centerline{\large{\bf LECTURE 14}}
\centerline{\bf Input and Output Streams}
Classes dealing with the input and output of data are called {\it stream}
classes. We have dealt with these classes in a slightly haphazard
way. I'd like to talk about them in a more systematic way to give
you a better idea of how input and output works. This won't be a complete
discussion since that would take too long. There is a chapter on
streams in Stroustrup and the first third of {\it More C++ for Dummies}
deals with streams. Most of this lecture comes from
{\it More C++ for Dummies}.
The class hierarchy is shown in the figure.

\epsfbox{lec13iotree.eps}

The mother of all base classes is {\tt ios}. (In more recent
compilers, {\tt ios} has been replaced by \verb2ios_base2.)
The class {\tt ios} contains
most of the actual I/O code. It is {\tt ios} that keeps track of the error
state of the stream. The error flags are an enumerated type within
{\tt ios}. In addition the {\tt ios} class converts data for display.
It understands the format of the different types of numbers, how to 
output character strings, and how to convert an ASCII string to and from
an integer or a floating--point number.

Standard output, {\tt cout}, is an object of the class {\tt ostream}, as is
{\tt cerr}, the standard error output. Standard input, {\tt cin}, 
is an object of the class {\tt istream}. {\tt cout}, {\tt cin},
and {\tt cerr} are automatically constructed as global objects
at program start--up. Objects of
{\tt iostream} deal with both input and output. Objects of {\tt ifstream}
deal with input files and objects of the class {\tt ofstream} deal with
output files. Objects {\tt fstream} deal with
files that can one can write to and read from. {\tt ofstream},
{\tt ifstream}, and {\tt fstream} are subclasses that are defined in the
include file {\tt fstream.h}. Notice that {\tt fstream.h}
deals with file stream classes.

The overloaded right shift operator \verb2operator>>()2 is called
the extractor. It is a member function of the class {\tt istream}.
The overloade left shift operator \verb2operator<<()2 is called
the inserter. It is a member function of the class {\tt ostream}.
Thus we have
\begin{verbatim}
//for input
istream& operator>>(istream& source, char *pDest);
istream& operator>>(istream& source, int &dest);
istream& operator>>(istream& source, char &dest);
//...etc...
//for output
ostream& operator<<(ostream& dest, char *pSource);
ostream& operator<<(ostream& dest, int source);
ostream& operator<<(ostream& dest, char source);
\end{verbatim}
So when we type
\begin{verbatim}
#include <iostream.h>

void fn()
{
  cout<<"Hello, world\n";
}
\end{verbatim}
First, C++ dtermines that the left--hand argument is of type
{\tt ostream} and the right--hand argument is of type {\tt char*}.
Armed with this knowledge, it finds the prototype 
\verb2operator<<(ostream&, char*)2 in {\tt iostream.h}. C++
generates a call to this function, the 
{\tt char*} inserter, passing the function the string
\verb2"Hello, world\n"2 and the object {\tt cout} as the two arguments.
That is, it makes the call \verb2operator<<(cout, "Hello, world\n")2.
The {\tt char*} inserter function, which is part of the standard
C++ library, performs the requested output.

\centerline{\bf Input and Output Files}
We have already seen that to open files to read from and write to:
\begin{verbatim}
  #include <fstream.h>
   
   int main() {
    ifstream infile("input.dat");     //input.dat is the name of the file
                                      //in your directory
    ofstream outfile("output.dat");   //output.dat is the name of the file
                                      //that will be created in your directory
    float x;
      while(infile >> x)              //detects end-of-file and exits loop
         { outfile << "x = " << x << endl; }
    infile.close();
    outfile.close();
    return 0;
   }
\end{verbatim}
The statement 
\begin{verbatim}
    ofstream outfile("output.dat");
\end{verbatim}
calls one of the constructors of {\tt ofstream}. It constructs the
object {\tt outfile} using the argument \verb2"output.dat"2.
The constructor that's called is 
\begin{verbatim}
ofstream::ofstream(const char *pFileName, 
                   int mode = ios::out, 
                   int prot = filebuff::openprot);
\end{verbatim}
The first argument is a pointer to the name of the file to open.
The second and third arguments specify how the file will be opened.
Since we didn't specify the second and third arguments, the default
values take effect. Similarly, the statement
\begin{verbatim}
    ifstream infile("input.dat");
\end{verbatim}
calls the following constructor of the class {\tt ifstream}:
\begin{verbatim}
ifstream::ifstream(const char *pFileName,
                   int mode = ios::in,
                   int prot = filebuff::openprot);
\end{verbatim}
The legal values for {\it mode} are listed in the following table.
The Application column indicates which modes are valid for which
file types.

\centerline{\bf Available Modes for Opening a File}
\begin{verbatim}
Flag          Application                        Meaning
____________________________________________________________
ios::app        out             Always append output to the end of the file
___________________________________________________________________________
ios::ate        out             Open and seek to end-of-file ("at end")
___________________________________________________________________________ 
ios::binary     in, out         Open file in binary mode (as opposed to
                                text mode)
___________________________________________________________________________
ios::in         in              Open a file for input
___________________________________________________________________________
ios::nocreate   in, out         If file doesn't exist, don't create it
___________________________________________________________________________
ios::noreplace  out             Don't delete the file (open fails if file
                                exists unless you specify app or ate)
___________________________________________________________________________
ios::out        out             Open file for output
___________________________________________________________________________
ios::trunc      out             Truncate file to zero length if it
                                already exists (default if file exists and
                                app or ate is not specified)
___________________________________________________________________________
\end{verbatim} 
These modes are bit fields that are enumerated members
of a bit vector in the class {\tt ios}. That is why they are
referred to as {\tt ios::out} and {\tt ios::app} and not simply
as {\tt out} and {\tt app}. Let me give an example of what I mean
by ``bit fields''. {\tt ios::app} might equal {\tt 00000001}, {\tt ios::ate}
might equal {\tt 00000010}, {\tt ios::out} might equal
{\tt 00000100}, etc. So each mode corresponds to one bit which
can be 0 or 1. This means that more than one mode's value 
can be set at the same time using the arithmetic OR. For example, to open
an output file with append, you could use the following:
\begin{verbatim}
ofstream out("outfile", ios::out | ios::app)
\end{verbatim}
The \verb2|2 operator takes the union of the two arguments.
Once you specify any part of the mode, you must specify the entire mode.
Thus, when I specified {\tt ios::app}, I also had to specify
{\tt ios::out}, because it was no longer specified by default.
The {\tt ios::nocreate} flag says ``If file doesn't exist, don't create it.''
This is especially useful for input. For example, it is the only way
to test for the existence of a file.

The third argument to the {\tt fstream} constructors specifies
the type of file sharing allowed between applications. The possible
values are:

\centerline{\bf File--Sharing Flags}
\begin{verbatim}
   Sharing Flag               Meaning
___________________________________________________________________
filebuf::openprot             Compatibility sharing mode
filebuf::sh_compat
___________________________________________________________________
filebuf::sh_none              Exclusive mode; no sharing allowed
___________________________________________________________________
filebuf::sh_read              Other read opens allowed
___________________________________________________________________
filebuf::sh_write             Other write opens allowed
___________________________________________________________________
\end{verbatim}
These are also bit fields. So the union of \verb2filebuf::sh_read2 and
\verb2filebuf::sh_write2 allows complete sharing of files between
applications. 

As we have seen in our earlier discussion of input and output,
it is also possible to open a file for both input and output.
This is handled by the {\tt fstream} class, which inherits from both
the {\tt ifstream} and {\tt ostream} classes. The constructor
for the {\tt fstream} class looks the same as those for the
{\tt ifstream} and {\tt ofstream} classes except the mode argument
is not defaulted:
\begin{verbatim}
  fstream::fstream(const char *pFileName,
                     int mode,
                     int prot = filebuf::openprot);
\end{verbatim}
To open such a file, the mode should be set to 
\verb2ios::in|ios::out2. For example,
\begin{verbatim}
  #include <fstream.h>

   int main() {
    fstream inout("input.dat",ios::in|ios::out);
    float x;
      inout >> x; 
      inout<< endl << "x = " << x << endl; 
    inout.close();
    return 0;
   }
\end{verbatim}

\centerline{\bf Error Flags}
If something goes wrong with the input/output (I/O) operations, the error
state is set. Once the error state is set, all subsequent requests
for I/O are ignored. The error state stays set until it is reset by
the application. This allows the application to perform several I/O
operations in row before checking--perhaps at the end of the function--
whether the I/O operations succeeded. The error flag consists of
a set of bits, each of which can be set independently. These bits
are defined as follows. (Note the values are provided to satisfy 
your curiosity. Don't rely on them. Always use the name of the flag
instead.)
\begin{verbatim}
  Flag         Binary Value       Meaning
____________________________________________________________________
ios::eofbit        001            End-of-file encountered
____________________________________________________________________
ios::failbit       010            Last I/O operation failed
____________________________________________________________________
ios::badbit        100            Invalid operation attempted
____________________________________________________________________
\end{verbatim}
When a read operation encounters the end--of--file, it sets the
{\tt ios::eofbit} in the error flag for the {\tt ifstream} object.
The {\tt ios::failbit} is set when an I/O operation fails. This could
happen if the format of the data is improper. For example, attempting
to extract a string of ASCII text into a number sets the {\tt failbit}.
In other words, if the program expects a number to be input and you give
it a string of letters, {\tt ios::failbit} is set. Attempting to read
beyond the end--of--file also sets the {\tt failbit}. Similar to the
{\tt ios::failbit}, the {\tt ios::badbit} is set when an operation
fails. The two flags differ in that the {\tt badbit} indicates an
unrecoverable error, whereas you might be able to recover from the 
{\tt failbit} (then again, maybe not, but at least you have a chance).
For example, attempting to open a file that doesn't exist sets the
{\tt badbit}. Although it's interesting to see which bits make up the
error flag, you don't actually ever check or set these bits directly.
Instead, you use the following access functions:

\centerline{\bf Functions that Read or Set the Stream Error State}
\begin{verbatim}
   Function              Purpose
_______________________________________________________________
ios::bad                 Returns TRUE if the badbit is set
_______________________________________________________________
ios::clear(int=0)        Sets the error flag
_______________________________________________________________
ios::eof()               Returns TRUE if the eofbit is set
_______________________________________________________________
ios::fail()              Returns TRUE if either the failbit or 
                         the badbit is set
_______________________________________________________________
ios::good()              Returns TRUE if no error bits are set
_______________________________________________________________
ios::rdstate()           Returns the error flag
_______________________________________________________________
ios::operator!()         Same as ios::fail()
_______________________________________________________________
ios::operator void*()    Same as ios::good()
_______________________________________________________________
\end{verbatim}
The check function are primarily {\tt ios::eof()}, {\tt ios::fail()},
and {\tt ios::bad()}. Each of these return TRUE fi their respective
bit is set (except {\tt ios::fail()}, which returns a TRUE if 
either the failbit or the badbit is set). The {\tt ios::good()} function
returns the inverse of {\tt ios::fail()}. Just about the worst named
function in the entire C++ library is {\tt ios::clear()}. This function
gets its name from the fact that you use it to clear the error flag. 
However, you also use it to set an error flag. In fact, {\tt clear()}
allows you to set or clear any of the error bits you want. The two
overloaded operators are just cute ways of calling {\tt ios::good()}
and {\tt ios::fail()}. For example,
\begin{verbatim}
  void fn(istream& in)
  {
     //stuff
     if(!in)      //invokes operator!(), which calls in.fail()
     {            
                  //operation failed
       return;
     }
   }
\end{verbatim}
Alternatively, we could write the following:
\begin{verbatim}
  void fn(istream& in)
  {
     //stuff
     if(in)      //invokes operator void*(), which calls in.good()
     {            
                  //operation succeeded
      //stuff
     }
   }
\end{verbatim}
Some programmers prefer this form of calling {\tt ios::good()}
and {\tt ios::fail()}. A simple example of error checking is
\begin{verbatim}
  #include <fstream.h>
   
   int main() {
    ifstream infile("input.dat");     //input.dat is the name of the file
                                      //in your directory
    ofstream outfile("output.dat");   //output.dat is the name of the file
                                      //that will be created in your directory
    float x;
      while(!infile.eof() && infile.good()) //makes sure that end-of-file
                                            //hasn't been reached and that
                                            //infile is in good shape 
         {
          infile >> x; 
          outfile << "x = " << x << endl; 
         }
    infile.close();
    outfile.close();
    return 0;
   }
\end{verbatim}
(This program outputs the last {\tt x} twice because the end--of--file
is not reached until it goes beyond the last item in {\tt input.dat}.)

\centerline{\bf Other Member Functions of istream and ostream}
The {\tt istream} and {\tt ostream} classes support a number of member
I/O functions in addition to the overloaded insertion and extraction
operators. Some of these follow:
\begin{description}
\item{\bf The get() Function}

The {\tt get()} function comes in two flavors. The simplest version inputs
a single character.  For example, in the following program snippet, 
{\tt get()} is used to read input from the input file input.txt:
\begin{verbatim}

  #include <fstream.h>
   
   int main() {
    ifstream in("input.txt");
    char c;
    while(!in.eof())
     {
       c = in.get();
       cout << c;
     }
    cout << endl;
   return 0;
   }
\end{verbatim}
The program starts by opening the file {\tt input.txt}. The program
then loops until the file is empty. On each loop, the program fetches 
another character and outputs it to the standard output. Notice
that {\tt get()} is not quite the same as \verb2operator>>(istream&,char&)2,
which also fetches a single character from the input stream. The 
difference lies in the fact that \verb2operator>>()2, by default,
skips any white--space characters (e.g., space, tab, newline, etc.)
found in the file, whereas {\tt get()} does not. In fact this program
also spits out a strange end--of--file character at the end.

The second version of {\tt get()}  carries the following prototype:
\begin{verbatim}
istream& istream::get(char* pszTarget, int nCount, char delim='\n');
\end{verbatim}
This version inputs a series of characters terminated either by the
appearance of a terminator character in the input stream or by a
character count. Notice that you can have any character you
want terminate the input. The count character solves the potential bug of
inputting more characters than the buffer can hold. For example,
the follwoing code is inherently unsafe because it is entirely
possible that the string extracted by \verb2operator>>()2 is
longer than the 80 characters the buffer can hold:
\begin{verbatim}
istream in("input.txt");
char buffer[80];
in >> buffer;
\end{verbatim}
A safer alternative would be 
\begin{verbatim}
istream in("input.txt");
char buffer[80];
in.get(buffer, 80);
\end{verbatim}
Because {\tt get()} now knows the length of the receiving buffer, it will
make sure not to extract more characters than the buffer can handle.
{\tt get()} gets not more than 80 characters and puts them into the
array {\tt buffer}.
You can have {\tt sizeof} calculate the buffer for you:
\begin{verbatim}
istream in("input.txt");
char buffer[80];
in.get(buffer, sizeof buffer);
\end{verbatim}
\item{\bf The getline() Function}

The prototype declaration for the {\tt getline()} function is identical
to that of the {\tt get()} function:
\begin{verbatim}
istream& istream::getline(char* pszTarget, int nCount, char delim='\n');
\end{verbatim}
In execution, {\tt getline()} is identical to the second form of 
{\tt get()}. The sole exception is that {\tt get()} extracts 
characters from the input stream up to, but not including, the delimter,
whereas {\tt getline()} extracts the delimiter as well. Neither function
stores the delimiter into the {\tt pszTarget} buffer. This makes
{\tt getline()} ideal fro reading an entire line of input at a time.
It reads this line of input from the input file and puts it in the
array pointed to by {\tt pszTarget}. 
\begin{verbatim}
  #include <fstream.h>

   int main() {
    ifstream in("input.txt");
    char szTarget[256];
       in.getline(szTarget, sizeof szTarget);
       cout << szTarget << "\n";
   return 0;
   }
\end{verbatim}
This program reads an entire line at a time. Notice that when the line
that was just read is output to {\tt cout}, the program must replace
the delimiter that was stripped out by {\tt getline()}.
\item {\bf The read() Function}

The prototype declaration for the read function is 
\begin{verbatim}
istream& read(char* pszTarget, int nCount);
\end{verbatim}
This function reads a fixed number of characters from the input stream
without regard to any type of delimiter. In addition, {\tt read()}
doesn't tack a NULL character to the end of the {\tt pszTarget} buffer,
nor does it attempt to interpret \verb2'\n'2 characters.
\item {\bf The put() Function}

The {\tt put()} function carries the following simple prototype:
\begin{verbatim}
ostream& ostream::put(char ch);
\end{verbatim}
This function does nothing more than output the specified character to
the output stream. This is functionally identical to the
\verb2operator<<(ostream&, char&)2 inserter.
\item{\bf The putback(Ch c) function}

The {\tt putback(Ch c)} function allows a program to put an
unwanted character back to be read some other time, as shown in
the class of complex numbers in lecture 6. {\tt Ch} is a template
type, i.e., it's any type you specify.
\item{\bf The write() Function}

The {\tt write()} function outputs a fixed number of characters
from the source character string to the output stream. This function
carries the following prototype
\begin{verbatim}  
ostream& ostream::write(const char* pszSource, int nCount);
\end{verbatim}
This is also a block--oriented transfer.

\end{description}

\centerline{\bf The Buffer}
We said earlier that the base class {\tt ios} does most of the
input/output work. But it needs another class called {\tt streambuf} which
acts as a server to the {\tt ios} class. {\tt streambuf} is an intermediatry
between {\tt ios} and the physical media, e.g., the screen, the disk, etc.
The class {\tt streambuf} performs the actual I/O to the outside world.
The class {\tt streambuf} has several subclasses, each of which 
specializes in its own particular type of media. For example, 
{\tt filebuf} handles file I/O for the {\tt ios} class. Look at the
following code:
\begin{verbatim}
ofstream out("ofile.txt");
int nAnInt = 10;
out << nAnInt;
\end{verbatim}
The constructor for {\tt ofstream} first creates an {\tt ios} object.
It then constructs a {\tt filebuf} object for output to the file
{\tt ofile.txt}. During output, the ios object converts the number
10 into the character 1 followed by the character 0. The {\tt ios}
object passes the string ``10'' to the {\tt filebuf} object for output
to the file. This is a nice division of labor. When you create a
different type of output object--for example an {\tt ostream} object
that outputs to the display--you get the same {\tt ios} base class
object (all formatting is the same, after all) but a different
subclass of {\tt streambuf} (outputting to a display is not at all
the same as outputting to a file).

It's worth taking a moment to understand what disk buffering is.
This is one of the functions performed by {\tt streambuf}. If 
{\tt streambuf} went to the disk every time {\tt ios} wanted to
write a few characters to disk, performance would be really slow.
In fact, when you read or write to the disk, you must read an entire
block of data at a time. (It's like Lay's Potato Chips--you can't eat
just one ``byte''.) The size of a block depends on the disk, but it's
usually 512 bytes or more. Therefore, on output, the {\tt streambuf}
class collects output requests in the buffer until it has several
blocks worth. It then writes the entire buffer to the disk at once.
Writing the output buffer to disk is called ``flushing the buffer.''
For example, {\tt endl} ends the line (\verb2"\n"2)
and then flushes the buffer.

On input, the situation is reversed. When the {\tt ios} class asks for
the first character from the input stream, the input buffer is empty.
Rather than read a single character (even if that were possible), the
{\tt streambuf} reads several blocks of data into the input
buffer. Then {\tt streambuf} returns only the first character to
{\tt ios} and keeps the rest. When the next input request comes in,
{\tt streambuf} returns the next character from the input buffer without
bothering to read from the disk. The {\tt streambuf} class doesn't read
from the disk again until the input buffer has been emptied by input
requests.

A few conditions cause the output buffer to be flushed to disk early.
For example, closing the file causes any remaining data that might
be hanging around in the buffer to be flushed to disk. The application
program can also force the output buffer to be flushed by calling
{\tt ostream::flush()}. For example,
\begin{verbatim}
  out << student;
  out.flush()
\end{verbatim}
This assures your data is safely on the disk in case the program or
the system crashes later on. Finally an output stream can be tied to
an input stream so that a request for I/O from the input stream 
immediately flushes the output stream. For example,
\begin{verbatim}
  char szname[80]'
  cout << "Enter your name";
  cin >> szName;
\end{verbatim}
Things wouldn't work so well if ``Enter your name'' didn't make it
to the display because it was cooling its heels in the output buffer.
Tying {\tt cout} to {\tt cin} flushes the output buffer so that
``Enter your name'' can appear on the screen.

\centerline{\bf Formatting}
It is often desirable to format your output by setting the precision
or the width, etc. You can do this using the following {\tt ios}
member functions:
\begin{verbatim}
    Function             Purpose
_______________________________________________________________
    fill(char)           Set the fill character for padding
                         during output.
_______________________________________________________________
    flags(long)          Set the formatting flags.
_______________________________________________________________
    precision(int)       Set the precision when outputting a 
                         floating-point value
_______________________________________________________________
    width(int)           Set the minimum width. Restricts
                         the number of characters that are
                         input. If fewer characters are required
                         on output, the remaining space is filled 
                         with the fill character.
\end{verbatim}
Each of these functions also has a {\tt void} argument list version,
which simply queries the current setting.
The {\tt fill()} function sets the fill character. The default for
the fill character is the blank space. The {\tt precision()} function
sets the precision. During the display of floating--point numbers, this
setting determines the number of digits displayed to the right of the
decimal point.

On output, the {\tt width()} function specifies the minimum field width
to be used in displaying the next field inserted. If the value being output
requires more characters than are specified by {\tt width()}, the
width is ignored. If the output field is smaller than the specified width,
the difference is made up by repeated application of the fill character. 
If the width is zero, the minimum number of characters necessary to
contain the field are used for output. Zero is the default for
the width. The {\tt width()} function is strange in one respect. When
you set a data member within a structure to a particular value,
you usually expect it to stay set. But this is not so for the width.
Each time you set the width, that setting applies only to the next
operation. After that, the width is reset to zero.
 
The {\tt width()} function also has an effect on the input. Setting the
width restricts the number of characters extracted by the {\tt char*}
extractor. This is important because the \verb2operator>>(istream&, char*)2,
unlike the {\tt getline()} function,
has no place to indicate the size of the buffer receiving the
character string. Settting the width to the size of the buffer ensures
that the buffer boundaries are not exceeded.

Here is an example of how to use a few of these format control functions:
\begin{verbatim}
  #include <fstream.h>

  int main()
  {
    ifstream infile("input.dat");
    if(infile.fail())
     {
      cout << "Couldn't open input.dat" << endl;
      return -1;
     }

    char buffer[5];                   //short array of characters
    infile.width(sizeof buffer);
    infile >> buffer;
    cout << buffer << endl;

    infile.width(sizeof buffer);      //have to repeat width()
    infile >> buffer;
    cout.width(15);
    cout.fill('*');
    cout << buffer << endl;
    return 0;
  }
\end{verbatim}
The program starts by opening the input file in the normal fashion. Before
extracting from the input file object, however, the program sets the
input width to match the size of the buffer. The program then extracts
a few characters into {\tt buffer} and displays them to {\tt cout} so
you can see what you have. Suppose the input file consists of 30
characters, all in a row.
\begin{verbatim}
//input.dat file
123456789012345678901234567890
\end{verbatim}
If we just had an {\tt in >> buffer} statement, the program would
try to put all 30 characters into the buffer which has length 5.
This would crash the program and give a ``bus error''. The output
from running the program is
\begin{verbatim}
1234
***********5678
\end{verbatim}
Notice that width the input stream width set to 5 characters, the
extractor reads only four characters, cleverly leaving a space for the
NULL in the final position of the buffer. 

The remaining formatting features of {\tt ios} are hidden in a protected
data member called \verb2x_flags2. This data member consists of a series
of 1--bit fields, some of which work together. Because these are
single--bit fields, however, they can be (and are) set in different
combinations to produce the desired effect. They are listed on pages
89--90 of {\it More C++ for Dummies}. The only ones that you would
probably use with any frequency are those dealing with floating--point
format. By setting either the {\tt ios::fixed} or {\tt ios::scientific}
flag, you specify whether floating--point numbers are displayed in
fixed or scientific format. If neither bit is set, the stream is 
in automatic mode, meaning use whatever is most applicable. In automatic
mode, the precision referred to previously specifies the number of digits
to be displayed in the number. In either fixed or scientific mode,
precision specifies the number of digits after the decimal point.
The default is automatic. Several functions access the flag
bits. The function {\tt long ios::flags()} reads the
current flag word. The function {\tt long ios::flags(long lNewFlag)}
sets the flag word to the value contained in {\tt lNewFlag}, and
returns the previous value. An example of how to use this is:
\begin{verbatim}
  long lPreviousFlags;
  lPreviousFlags = cout.flags();          //record current flag word
  cout.flags(cout.flags() | ios::fixed);      //fixed floating point notation
  cout << "7 = " << 7.0 << endl;
  cout.flags(lPreviousFlags);                 //reset flags to previous value
  cout.flags(cout.flags() | ios::scientific); //scientific notation
  cout << "7 = " << 7.0 << endl;
\end{verbatim}
We write 
\begin{verbatim}
  cout.flags(cout.flags() | ios::fixed);
\end{verbatim}
so that we have the union of fixed floating point notation with
the flags that have already been set. If we had written
\begin{verbatim}
  cout.flags(ios::fixed);
\end{verbatim}
we would have wiped out the all the other format flags that had been 
set, even the default ones. There are several other functions that you
might find more convenient than {\tt flags()}. For example, the 
{\tt ios::setf(long)} function sets a particular flag, and the
{\tt ios::unsetf(long)} function clears that flag. So to set the
scientific notation flag, I could write:
\begin{verbatim}
  cout.setf(ios::scientific);
\end{verbatim}
To clear it, I could write:
\begin{verbatim}
  cout.unsetf(ios::scientific);
\end{verbatim}

\centerline{\bf Manipulators}

Using {\tt ios} member functions breaks up the flow of the output line.
It's not very elegant to write
\begin{verbatim}
 cout << "I = ";
 cout.width(10);
 cout << i << endl;
\end{verbatim}
It's prettier to write
\begin{verbatim}
 cout << "I = " << i << endl;
\end{verbatim}
To overcome this problem, one can use a manipulator. {\it Manipulators}
are objects defined in the include file {\tt iomanip.h} to have
the same effect as the member functions calls. In fact, they
call the functions. The only
advantage to manipulators is that the program can insert them
directly into the stream rather than resort to a separate function call.
Some manipulators take arguments and some do not. 
Technically speaking, you only have to include the header file
{\tt iomanip.h} in order to use the manipulators with arguments.
Some of the manipulators are listed here. A more complete list can
be found on page 100 of {\it More C++ for Dummies}.
\begin{verbatim}
 Manipulator      Member Functions    Purpose
__________________________________________________________________
  endl            ostream::flush()    Insert \n and flush the buffer
__________________________________________________________________
  flush           ostream::flush()    Flush the stream
__________________________________________________________________
  setfill()       ios::fill()         Set the fill character
__________________________________________________________________
  setiosflags()   ios::setf()         Set the flags
__________________________________________________________________
  resetiosflags() ios::unsetf()       Undo the flags
__________________________________________________________________
  setprecision()  ios::precision()    Set the floating-point precision
__________________________________________________________________
  setw()          ios::width()        Set the width of the next field
\end{verbatim}
Just like the {\tt width()} function, the {\tt setw()} manipulator
must be included in the stream for each object whose width is not
the default, zero. The only advantage that function calls have over
manipulators is that the functions return the previous settings while
the manipulators don't return anything. So if you want to store
the previous setting, do something, then restore the previous setting,
you can easily do it with function calls. With manipulators, you
can only undo the flags with {\tt resetiosflags()}. Here is an
example using manipulators:
\begin{verbatim}
  #include <iostream.h>
  #include <iomanip.h>

  void fn()
  {
   cout << setw(8) << 10 << setw(8) << 20 << endl;  //keep setting width
   cout << setiosflags(ios::scientific)             
        << "7.0 = " << 7.0 << endl;        //scientific notation
   cout << resetiosflags(ios::scientific); //turn off scientific notation
  }
\end{verbatim}

\centerline{\bf Custom Inserters}

The fact that C++ overloads the left shift operator to perform output
means that you can overload the same operator to perform output on
classes you define. We have seen examples of this. Recall that the class
of complex numbers in lecture 6 had a friend function that overloaded
the inserter:
\begin{verbatim}
ostream & operator << (ostream &, const complex &);
\end{verbatim}
Let's give another example with the class {\tt USDollar}:
\begin{verbatim}
#include <iostream.h>
#include <iomanip.h>
class USDollar
{
  public:
   USDollar(double v = 0.0)
   {
      dollars = v;
      cents = int((v - dollars) * 100.0 + 0.5);
   }
   operator double()
   {
      return dollars + cents / 100.0;
   }
   void display(ostream& out)
   {
      out << '$' << dollars << '.'
          //set fill to 0's for cents
          << setfill('0') << setw(2) << cents
          //now put it back to spaces
          << setfill(' ');
   }
  protected:
   unsigned int dollars;
   unsigned int cents;
};

//operator<< - overload the inserter for our class
ostream& operator<< (ostream& o, const USDollar& d)
{
   d.display(o);
   return o;
}
int main()
{
   USDollar usd(1.50);
   cout << "Initially usd = " << usd << "\n";
   usd = 2.0 * usd;
   cout << "then usd = " << usd << "\n";
   return 0;
}
\end{verbatim}
The {\tt display()} function starts by displaying \verb2$2, the dollar
amount, and the obligatory decimal point. Notice that output is to 
whatever {\tt ostream} object it is passed and not necessarily just to
{\tt cout}. This allows the same function to be used on objects of
{\tt ostream} and its subclasses such as {\tt fstream}. When it comes
time to display the cents amount, {\tt display()} sets the
width to 2 positions and the leading character to 0. This ensures
that numbers smaller than 10 display properly. Notice how the class
{\tt USDollar}, instead of accessing the {\tt display()} function
directly, also defines an \verb2operator<<(ostream&, USDollar&)2. The 
programmer can now output {\tt USDollar} objects as easily as intrinsic
types, as the example in {\tt main()} demonstrates. The output from the
program is:
\begin{verbatim}
Initially usd = $1.50
then usd = $3.00
\end{verbatim}
Notice that
the \verb2operator<<()2 returns the {\tt ostream} passed to it. 
This allows the operator to be chained with other inserters in a 
single expression, i.e, this allows us to string output operators together:
\begin{verbatim}
   complex c, d, x;
   ostream s;
   s << c << d << x;
\end{verbatim}
Because the \verb2operator<<()2 binds left to right, the expression
\begin{verbatim}
      s << c << d << x;
\end{verbatim}
can be interpreted as
\begin{verbatim}
      (((s << c) << d) << x);
\end{verbatim}
The first insertion outputs the complex number {\tt c} to 
{\tt s}. The result of this expression is the object {\tt s},
which is then passed to \verb2operator<<(ostream &, const complex &)2.
It is important that this opertor return its {\tt ostream} object
so that the object can be passed to the next inserter in turn.

\centerline{\bf Smart Inserters}

Many times, you would like to make the inserter smart. That is, you would 
like to say \verb2cout << baseClassObject2 and let C++ choose the proper
subclass inserter in the same way that it choose the proper virtual
member function. Because the inserter is not a member function, you
cannot declare it virtual directly. There is a clever way to get 
around this:
\begin{verbatim}
#include <iostream.h>
#include <iomanip.h>
class Currency
{
  public:
   Currency(double v = 0.0)
   {
      unit = v;
      cent = int((v - unit) * 100.0 + 0.5);
   }
   virtual void display(ostream& out) = 0;

  protected:
   unsigned int unit;
   unsigned int cent;
};
class USDollar : public Currency
{
  public:
   USDollar(double v = 0.0) : Currency(v)
   {
   }
   //display $123.00
   virtual void display(ostream& out)
   {
      out << '$' << unit << '.'
          << setfill('0') << setw(2) << cent
          << setfill(' ');
   }
};
class DMark : public Currency
{
  public:
   DMark(double v = 0.0) : Currency(v)
   {
   }
   //display 123.00DM
   virtual void display(ostream& out)
   {
      out << unit << '.'
          //set fill to 0's for cents
          << setfill('0') << setw(2) << cent
          //now put it back to spaces
          << setfill(' ')
          << " DM";
   }
};
ostream& operator<< (ostream& o, Currency& c)
{
   c.display(o);
   return o;
}
void fn(Currency& c)
{
   //the following output is polymorphic because
   //operator(ostream&, Currency&) is through a virtual
   //member function
   cout << "Deposit was " << c
        << "\n";
}
int main()
{
   //create a dollar and output it using the
   //proper format for a dollar
   USDollar usd(1.50);
   fn(usd);

   //now create a DMark and output it using its own format
   DMark d(3.00);
   fn(d);
   return 0;
}
\end{verbatim}
The class {\tt Currency} has two subclasses, {\tt USDollar} and {\tt DMark}.
In {\tt Currency}, the {\tt display()} function is declared pure virtual.
In each of the two subclasses, this function is overloaded with a
{\tt display()} function to output the object in the proper format
for that type. The call to {\tt display()} in \verb2operator<<()2 is
now a virtual call. Thus, when \verb2operator<<()2 is passed 
{\tt USDollar}, it outputs the object as a dollar. When passed
{\tt DMark}, it outputs the object as a deutsche mark. Thus, although
\verb2operator<<()2 is not virtual, because it invokes a virtual function,
it acts like a virtual function and the result is:
\begin{verbatim}
Deposit was $1.50
Deposit was 3.00 DM
\end{verbatim}
This is another reason why it is better to perform the work of output
in a member function, and let the non-member function refer to that
function.



\end{document}
