⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch16.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!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://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch15.htm"><IMG SRC="BLANPREV.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANPREV.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="tppmsgs/msgs0.htm#1" tppabs="http://www.mcp.com/sams"><IMGSRC="BLANHOME.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"BORDER="0"></A><A HREF="index.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/index.htm"><IMG SRC="BLANTOC.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANTOC.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch17.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch17.htm"><IMG SRC="BLANNEXT.GIF" tppabs="http://www.mcp.com/814147200/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&gt;&gt; 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&amp;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 screenor to a file, nor how data is read into a program. These are clearly essential partsof 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 andhandled in libraries is that it is easier to make the language &quot;platform-independent.&quot;That is, you can write C++ programs on a PC and then recompile them and run themon 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 screenas being a stream of data, one byte following another. If the destination of thestream 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 andbe &quot;poured&quot; into your data variables.</P><P>One principal goal of streams is to encapsulate the problems of getting the datato and from the disk or the screen. Once a stream is created, your program workswith the stream and the stream sweats the details. Figure 16.1 illustrates this fundamentalidea.<BR><BR><A NAME="Heading5"></A><A HREF="16zcp01.jpg" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/art/ch16/16zcp01.jpg"><FONT COLOR="#000077">Figure16.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 &quot;expensive.&quot;It takes a long time (relatively speaking) to write data to the disk or to read datafrom the disk, and execution of the program is generally blocked by disk writes andreads. To solve this problem, streams provide &quot;buffering.&quot; Data is writteninto 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 allat 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 flowsout in a rush. Figure 16.3 illustrates this.</P><P>Once the buffer is empty, the bottom valve closes, the top valve opens, and morewater 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 itis full. This is called &quot;flushing the buffer.&quot; Figure 16.5 illustratesthis idea.<BR><BR><A NAME="Heading7"></A><A HREF="16zcp02a.jpg" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/art/ch16/16zcp02a.jpg"><FONT COLOR="#000077">Figure16.2.</FONT></A><FONT COLOR="#000077"> </FONT><I>Filling the buffer.</I><BR><BR><A NAME="Heading8"></A><A HREF="16zcp02b.jpg" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/art/ch16/16zcp02b.jpg"><FONT COLOR="#000077">Figure16.3.</FONT></A><FONT COLOR="#000077"> </FONT><I>Emptying the buffer</I>.<BR><BR><A NAME="Heading9"></A><A HREF="16zcp02c.jpg" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/art/ch16/16zcp02c.jpg"><FONT COLOR="#000077">Figure16.4.</FONT></A><FONT COLOR="#000077"> </FONT><I>Refilling the buffer.</I><BR><BR><A NAME="Heading10"></A><A HREF="16zcp02d.jpg" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/art/ch16/16zcp02d.jpg"><FONT COLOR="#000077">Figure16.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 streamsand 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 objectsare 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 &quot;see-in&quot;) handles input from the standard	input, the keyboard.	<P>	<LI><TT>cou</TT> (pronounced &quot;see-out&quot;) handles output to the standard	output, the screen.	<P>	<LI><TT>cer</TT> (pronounced &quot;see-err&quot;) handles unbuffered output to the	standard error device, the screen. Because this is unbuffered, everything sent to	<TT>cerr</TT> is written to the standard error device immediately, without waiting	for the buffer to fill or for a flush command to be received.	<P>	<LI><TT>clo</TT> (pronounced &quot;see-log&quot;) handles buffered error messages	that are output to the standard error device, the screen. It is common for this to	be &quot;redirected&quot; to a log file, as described in the following section.</UL><H3 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Redirection</FONT></H3><P>Each of the standard devices, input, output, and error, can be redirected to otherdevices. Standard error is often redirected to a file, and standard input and outputcan be piped to files using operating system commands.</P><DL>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -