?? ch16.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Teach Yourself C++ in 21 Days</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1 ALIGN="CENTER"><A HREF="ch15.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch15.htm"><IMG SRC="../buttons/BLANPREV.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANPREV.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="javascript:if(confirm('http://www.mcp.com/sams \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mcp.com/sams'" tppabs="http://www.mcp.com/sams"><IMG
SRC="../buttons/BLANHOME.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
BORDER="0"></A><A HREF="../index.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/index.htm"><IMG SRC="../buttons/BLANTOC.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANTOC.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch17.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch17.htm"><IMG SRC="../buttons/BLANNEXT.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANNEXT.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A></H1>
<H1></H1>
<UL>
<LI><A HREF="#Heading1">Day 16</A>
<UL>
<LI><A HREF="#Heading2">Streams</A>
<UL>
<LI><A HREF="#Heading3">Overview of Streams</A>
<UL>
<LI><A HREF="#Heading4">Encapsulation</A>
<UL>
<LI><A HREF="#Heading5">Figure 16.1.</A>
</UL>
<LI><A HREF="#Heading6">Buffering</A>
<UL>
<LI><A HREF="#Heading7">Figure 16.2.</A>
<LI><A HREF="#Heading8">Figure 16.3.</A>
<LI><A HREF="#Heading9">Figure 16.4.</A>
<LI><A HREF="#Heading10">Figure 16.5.</A>
</UL>
</UL>
<LI><A HREF="#Heading11">Streams and Buffers</A>
<LI><A HREF="#Heading12">Standard I/O Objects</A>
<LI><A HREF="#Heading13">Redirection</A>
<LI><A HREF="#Heading14">Input Using cin</A>
<LI><A HREF="#Heading15">Listing 16.1. cin handles different data types</A><A HREF="#Heading16">.</A>
<UL>
<LI><A HREF="#Heading17">Strings</A>
<LI><A HREF="#Heading18">String Problems</A>
</UL>
<LI><A HREF="#Heading19">Listing 16.2. Trying to write more than one word to cin</A><A
HREF="#Heading20">.</A>
<LI><A HREF="#Heading21">Listing 16.3. Multiple input</A><A HREF="#Heading22">.</A>
<UL>
<LI><A HREF="#Heading23">operator>> Returns a Reference to an istream Object</A>
</UL>
<LI><A HREF="#Heading24">Other Member Functions of cin</A>
<UL>
<LI><A HREF="#Heading25">Single Character Input</A>
</UL>
<LI><A HREF="#Heading26">Listing 16.4. Using get() with no parameters.</A>
<LI><A HREF="#Heading27">Listing 16.5 Using get() with parameters.</A>
<UL>
<LI><A HREF="#Heading28">Getting Strings from Standard Input</A>
</UL>
<LI><A HREF="#Heading29">Listing 16.6. Using get() with a character array</A>
<LI><A HREF="#Heading30">.</A>
<LI><A HREF="#Heading31">Listing 16.7. Using getline().</A>
<UL>
<LI><A HREF="#Heading32">Using cin.ignore()</A>
</UL>
<LI><A HREF="#Heading33">Listing 16.8. Using ignore().</A>
<UL>
<LI><A HREF="#Heading34">peek() and putback()</A>
</UL>
<LI><A HREF="#Heading35">Listing 16.9. Using peek() and putback().</A>
<LI><A HREF="#Heading36">Output with cout</A>
<UL>
<LI><A HREF="#Heading37">Flushing the Output</A>
</UL>
<LI><A HREF="#Heading38">Related Functions</A>
<LI><A HREF="#Heading39">Listing 16.10. Using put().</A>
<LI><A HREF="#Heading40">Listing 16.11. Using write().</A>
<LI><A HREF="#Heading41">Manipulators, Flags, and Formatting Instructions</A>
<UL>
<LI><A HREF="#Heading42">Using cout.width()</A>
</UL>
<LI><A HREF="#Heading43">Listing 16.12. Adjusting the width of output.</A>
<UL>
<LI><A HREF="#Heading44">Setting the Fill Characters</A>
</UL>
<LI><A HREF="#Heading45">Listing 16.13. Using fill()</A><A HREF="#Heading46">.</A>
<UL>
<LI><A HREF="#Heading47">Set Flags</A>
</UL>
<LI><A HREF="#Heading48">Listing 16.14. Using setf.</A>
<LI><A HREF="#Heading49">Streams Versus the printf() Function</A>
<LI><A HREF="#Heading50">Listing 16.15. Printing with printf().</A>
<LI><A HREF="#Heading51">File Input and Output</A>
<LI><A HREF="#Heading52">ofstream</A>
<UL>
<LI><A HREF="#Heading53">Condition States</A>
<LI><A HREF="#Heading54">Opening Files for Input and Output</A>
</UL>
<LI><A HREF="#Heading55">Listing 16.16. Opening files for read and write</A><A HREF="#Heading56">.</A>
<UL>
<LI><A HREF="#Heading57">Changing the Default Behavior of ofstream on Open</A>
</UL>
<LI><A HREF="#Heading58">Listing 16.17. Appending to the end of a file</A>
<LI><A HREF="#Heading59">.</A>
<LI><A HREF="#Heading60">Binary Versus Text Files</A>
<LI><A HREF="#Heading61">Listing 16.18. Writing a class to a file</A><A HREF="#Heading62">.</A>
<LI><A HREF="#Heading63">Command-Line Processing</A>
<LI><A HREF="#Heading64">Listing 16.19. Using command-line arguments</A><A HREF="#Heading65">.</A>
<LI><A HREF="#Heading66">Listing 16.20. Using command-line arguments.</A>
<LI><A HREF="#Heading67">Summary</A>
<LI><A HREF="#Heading68">Q&A</A>
<LI><A HREF="#Heading69">Workshop</A>
<UL>
<LI><A HREF="#Heading70">Quiz</A>
<LI><A HREF="#Heading71">Exercises</A>
</UL>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 16</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Streams</FONT></H2>
<P>Until now, you've been using <TT>cout</TT> to write to the screen and <TT>cin</TT>
to read from the keyboard, without a full understanding of how they work. Today,
you will learn
<UL>
<LI>What streams are and how they are used.
<P>
<LI>How to manage input and output using streams.
<P>
<LI>How to write to and read from files using streams.
</UL>
<H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">Overview of Streams</FONT></H3>
<P>C++ does not, as part of the language, define how data is written to the screen
or to a file, nor how data is read into a program. These are clearly essential parts
of working with C++, however, and the standard C++ library now includes the <TT>iostream</TT>
library, which facilitates input and output (I/O).</P>
<P>The advantage of having the input and output kept apart from the language and
handled in libraries is that it is easier to make the language "platform-independent."
That is, you can write C++ programs on a PC and then recompile them and run them
on a Sun Workstation. The compiler manufacturer just supplies the right library,
and everything works. At least that's the theory.
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>A library is a collection of OBJ
files that can be linked to your program to provide additional functionality. This
is the most basic form of code reuse, and has been around since ancient programmers
chiseled 1s and 0s into the walls of caves.
<HR>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading4"></A><FONT COLOR="#000077">Encapsulation</FONT></H4>
<P>The <TT>iostream</TT> classes view the flow of data from your program to the screen
as being a stream of data, one byte following another. If the destination of the
stream is a file or the screen, the source is usually some part of your program.
If the stream is reversed, the data can come from the keyboard or a disk file and
be "poured" into your data variables.</P>
<P>One principal goal of streams is to encapsulate the problems of getting the data
to and from the disk or the screen. Once a stream is created, your program works
with the stream and the stream sweats the details. Figure 16.1 illustrates this fundamental
idea.<BR>
<BR>
<A NAME="Heading5"></A><A HREF="javascript:if(confirm('http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp01.jpg \n\nThis file was not retrieved by Teleport Pro, because the server reports that this file cannot be found. \n\nDo you want to open it from the server?'))window.location='http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp01.jpg'" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp01.jpg"><FONT COLOR="#000077">Figure
16.1.</FONT></A><FONT COLOR="#000077"> </FONT><I>Encapsulation through streams.</I>
<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Buffering</FONT></H4>
<P>Writing to the disk (and to a lesser extent the screen) is very "expensive."
It takes a long time (relatively speaking) to write data to the disk or to read data
from the disk, and execution of the program is generally blocked by disk writes and
reads. To solve this problem, streams provide "buffering." Data is written
into the stream, but it is not written back out to the disk immediately. Instead,
the stream's buffer fills and fills, and when it is full it writes to the disk all
at once.</P>
<P>Picture water trickling into the top of a tank, and the tank filling and filling,
but no water running out of the bottom. Figure 16.2 illustrates this idea.</P>
<P>When the water (data) reaches the top, the valve opens and all the water flows
out in a rush. Figure 16.3 illustrates this.</P>
<P>Once the buffer is empty, the bottom valve closes, the top valve opens, and more
water flows into the buffer tank. Figure 16.4 illustrates this.</P>
<P>Every once in a while you need to get the water out of the tank even before it
is full. This is called "flushing the buffer." Figure 16.5 illustrates
this idea.<BR>
<BR>
<A NAME="Heading7"></A><A HREF="javascript:if(confirm('http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02a.jpg \n\nThis file was not retrieved by Teleport Pro, because the server reports that this file cannot be found. \n\nDo you want to open it from the server?'))window.location='http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02a.jpg'" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02a.jpg"><FONT COLOR="#000077">Figure
16.2.</FONT></A><FONT COLOR="#000077"> </FONT><I>Filling the buffer.</I><BR>
<BR>
<A NAME="Heading8"></A><A HREF="javascript:if(confirm('http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02b.jpg \n\nThis file was not retrieved by Teleport Pro, because the server reports that this file cannot be found. \n\nDo you want to open it from the server?'))window.location='http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02b.jpg'" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02b.jpg"><FONT COLOR="#000077">Figure
16.3.</FONT></A><FONT COLOR="#000077"> </FONT><I>Emptying the buffer</I>.<BR>
<BR>
<A NAME="Heading9"></A><A HREF="javascript:if(confirm('http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02c.jpg \n\nThis file was not retrieved by Teleport Pro, because the server reports that this file cannot be found. \n\nDo you want to open it from the server?'))window.location='http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02c.jpg'" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02c.jpg"><FONT COLOR="#000077">Figure
16.4.</FONT></A><FONT COLOR="#000077"> </FONT><I>Refilling the buffer.</I><BR>
<BR>
<A NAME="Heading10"></A><A HREF="javascript:if(confirm('http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02d.jpg \n\nThis file was not retrieved by Teleport Pro, because the server reports that this file cannot be found. \n\nDo you want to open it from the server?'))window.location='http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02d.jpg'" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch16/16zcp02d.jpg"><FONT COLOR="#000077">Figure
16.5.</FONT></A><FONT COLOR="#000077"> </FONT><I>Flushing the buffer.</I>
<H3 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Streams and Buffers</FONT></H3>
<P>As you might expect, C++ takes an object-oriented view toward implementing streams
and buffers.
<UL>
<LI>The <TT>streambuf</TT> class manages the buffer, and its member functions provide
the capability to fill, empty, flush, and otherwise manipulate the buffer.
<P>
<LI>The <TT>ios</TT> class is the base class to the input and output stream classes.
The <TT>ios</TT> class has a <TT>streambuf</TT> object as a member variable.
<P>
<LI>The <TT>istream</TT> and <TT>ostream</TT> classes derive from the <TT>ios</TT>
class and specialize input and output stream behavior, respectively.
<P>
<LI>The <TT>iostream</TT> class is derived from both the <TT>istream</TT> and the
<TT>ostream</TT> classes and provides input and output methods for writing to the
screen.
<P>
<LI>The <TT>fstream</TT> classes provide input and output from files.
</UL>
<H3 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">Standard I/O Objects</FONT></H3>
<P>When a C++ program that includes the <TT>iostream</TT> classes starts, four objects
are created and initialized:
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>The <TT>iostream</TT> class library
is added automatically to your program by the compiler. All you need to do to use
these functions is to put the appropriate <TT>include</TT> statement at the top of
your program listing.
<HR>
</BLOCKQUOTE>
<UL>
<LI><TT>cin</TT> (pronounced "see-in") handles input from the standard
input, the keyboard.
<P>
<LI><TT>cou</TT> (pronounced "see-out") handles output to the standard
output, the screen.
<P>
<LI><TT>cer</TT> (pronounced "see-err") handles unbuffered output to the
standard error device, the screen. Because this is unbuffered, everything sent to
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -