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

📄 chap05.htm

📁 C++编程思想第二版第二卷
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<BLOCKQUOTE><FONT SIZE = "+1"><PRE> cout &lt;&lt; flush;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There are additional basic manipulators
that will change the number base to <B>oct</B> (octal)<A NAME="Index212"></A>,
<B>dec</B> (decimal)<A NAME="Index213"></A><A NAME="Index214"></A> or <B>hex</B>
(hexadecimal)<A NAME="Index215"></A><A NAME="Index216"></A>:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE> cout &lt;&lt; hex &lt;&lt; <font color=#004488>"0x"</font> &lt;&lt; i &lt;&lt; endl;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There&#8217;s a manipulator for
extraction that &#8220;eats&#8221; white space:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>cin &gt;&gt; ws;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">and a manipulator called
<B>ends<A NAME="Index217"></A></B>, <A NAME="Index218"></A>which is like
<B>endl</B>, only for strstreams (covered in a while). These are all the
manipulators in <B>&lt;iostream&gt;</B>, but there are more in
<B>&lt;iomanip&gt; </B>you&#8217;ll see later in the
chapter.</FONT><A NAME="_Toc312373879"></A><A NAME="_Toc519041947"></A><BR></P></DIV>
<A NAME="Heading112"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H3 ALIGN="LEFT">
Common usage</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Although <B>cin</B> and the extractor
<B>&gt;&gt;</B> provide a nice balance to <B>cout</B> and the inserter
<B>&lt;&lt;</B>, in practice using formatted input routines, especially with
standard input, has the same problems you run into with <B>scanf(&#160;)</B>. If
the input produces an unexpected value, the process is skewed, and it&#8217;s
very difficult to recover. In addition, formatted input defaults to whitespace
delimiters. So if you collect the above code fragments into a
program</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C05:Iosexamp.cpp</font>
<font color=#009900>// Iostream examples</font>
#include &lt;iostream&gt;
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;

<font color=#0000ff>int</font> main() {
  <font color=#0000ff>int</font> i;
  cin &gt;&gt; i;

  <font color=#0000ff>float</font> f;
  cin &gt;&gt; f;

  <font color=#0000ff>char</font> c;
  cin &gt;&gt; c;

  <font color=#0000ff>char</font> buf[100];
  cin &gt;&gt; buf;

  cout &lt;&lt; <font color=#004488>"i = "</font> &lt;&lt; i &lt;&lt; endl;
  cout &lt;&lt; <font color=#004488>"f = "</font> &lt;&lt; f &lt;&lt; endl;
  cout &lt;&lt; <font color=#004488>"c = "</font> &lt;&lt; c &lt;&lt; endl;
  cout &lt;&lt; <font color=#004488>"buf = "</font> &lt;&lt; buf &lt;&lt; endl;

  cout &lt;&lt; flush;
  cout &lt;&lt; hex &lt;&lt; <font color=#004488>"0x"</font> &lt;&lt; i &lt;&lt; endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">and give it the following
input,</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>12 1.4 c <font color=#0000ff>this</font> is a test</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">you&#8217;ll get the same output as if
you give it</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>12
1.4
c
<font color=#0000ff>this</font> is a test</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">and the output is, somewhat
unexpectedly,</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>i = 12
f = 1.4
c = c
buf = <font color=#0000ff>this</font>
0xc</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Notice that <B>buf</B> got only the first
word because the input routine looked for a space to delimit the input, which it
saw after &#8220;this.&#8221; In addition, if the continuous input string is
longer than the storage allocated for <B>buf</B>, you&#8217;ll overrun the
buffer.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It seems <B>cin</B> and the extractor are
provided only for completeness, and this is probably a good way to look at it.
In practice, you&#8217;ll usually want to get your input a line at a time
<A NAME="Index219"></A><A NAME="Index220"></A>as a sequence of characters and
then scan them and perform conversions once they&#8217;re safely in a buffer.
This way you don&#8217;t have to worry about the input routine choking on
unexpected data.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Another thing to consider is the whole
concept of a command-line
interface<A NAME="Index221"></A><A NAME="Index222"></A>. This has made sense in
the past when the console was little more than a glass typewriter, but the world
is rapidly changing to one where the graphical user interface (GUI)
<A NAME="Index223"></A><A NAME="Index224"></A><A NAME="Index225"></A>dominates.
What is the meaning of console I/O
<A NAME="Index226"></A><A NAME="Index227"></A>in such a world? It makes much
more sense to ignore <B>cin</B> altogether other than for very simple examples
or tests, and take the following approaches:</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Verdana">	</FONT><FONT FACE="Georgia">If your program requires
input, read that input from a file &#8211; you&#8217;ll soon see it&#8217;s
remarkably easy to use files with iostreams.
Iostreams</FONT></OL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">
<A NAME="Index228"></A><A NAME="Index229"></A>for files still works fine with a
GUI.</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Verdana">	</FONT><FONT FACE="Georgia">Read the input without
attempting to convert it. Once the input is someplace where it can&#8217;t foul
things up during conversion, then you can safely scan
it.</FONT><LI><FONT FACE="Verdana">	</FONT><FONT FACE="Georgia">Output is
different. If you&#8217;re using a GUI, <B>cout</B> doesn&#8217;t work and you
must send it to a file (which is identical to sending it to <B>cout</B>) or use
the GUI facilities for data display. Otherwise it often makes sense to send it
to <B>cout</B>. In both cases, the output formatting functions of iostreams are
highly
useful.</FONT><A NAME="_Toc312373880"></A><A NAME="_Toc519041948"></A></OL><A NAME="Heading113"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H3 ALIGN="LEFT">
Line-oriented input</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To grab input a line at a time, you have
two choices: the member functions <B>get(&#160;)</B> <A NAME="Index230"></A>and
<B>getline(&#160;)<A NAME="Index231"></A></B>. Both functions take three
arguments: a pointer to a character buffer in which to store the result, the
size of that buffer (so they don&#8217;t overrun it), and the terminating
character, to know when to stop reading input. The terminating character has a
default value of <B>&#8216;\n&#8217;</B>, which is what you&#8217;ll usually
use. Both functions store a zero in the result buffer when they encounter the
terminating character in the input.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">So what&#8217;s the difference? Subtle,
but important: <B>get(&#160;)</B> stops when it <I>sees</I> the delimiter in the
input stream, but it doesn&#8217;t extract it from the input stream. Thus, if
you did another <B>get(&#160;)</B> using the same delimiter it would immediately
return with no fetched input. (Presumably, you either use a different delimiter
in the next <B>get(&#160;)</B> statement or a different input function.)
<B>getline(&#160;)</B>, on the other hand, extracts the delimiter from the input
stream, but still doesn&#8217;t store it in the result buffer.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Generally, when you&#8217;re processing a
text file that you read a line at a time, you&#8217;ll want to use
<B>getline(&#160;)</B>.</FONT><BR></P></DIV>
<A NAME="Heading114"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H4 ALIGN="LEFT">
Overloaded versions of get(&#160;)</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>get(&#160;)</B> also comes in three
other overloaded versions<A NAME="Index232"></A>: one with no arguments that
returns the next character, using an <B>int</B> return value; one that stuffs a
character into its <B>char</B> argument, using a <I>reference</I> (You&#8217;ll
have to jump forward to Chapter XX if you want to understand it right this
minute . . . .); and one that stores directly into the underlying buffer
structure of another iostream object. That is explored later in the
chapter.</FONT><BR></P></DIV>
<A NAME="Heading115"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H4 ALIGN="LEFT">
Reading raw
bytes<BR><A NAME="Index233"></A><A NAME="Index234"></A><A NAME="Index235"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you know exactly what you&#8217;re
dealing with and want to move the bytes directly into a variable, array, or
structure in memory, you can use <B>read(&#160;)<A NAME="Index236"></A></B>. The
first argument is a pointer to the destination memory, and the second is the
number of bytes to read. This is especially useful if you&#8217;ve previously
stored the information to a file, for example, in binary form using the
complementary <B>write(&#160;)<A NAME="Index237"></A></B> member function for an
output stream. You&#8217;ll see examples of all these functions
later.</FONT><BR></P></DIV>
<A NAME="Heading116"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H4 ALIGN="LEFT">
Error handling<BR><A NAME="Index238"></A><A NAME="Index239"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">All the versions of <B>get(&#160;)</B>
and <B>getline(&#160;)</B> return the input stream from which the characters
came <I>except</I> for <B>get(&#160;)</B> with no arguments, which returns the
next character or EOF. If you get the input stream object back, you can ask it
if it&#8217;s still OK. In fact, you can ask <I>any</I> iostream object if
it&#8217;s OK using the member functions
<B>good(&#160;)<A NAME="Index240"></A><A NAME="Index241"></A></B>,
<B>eof(&#160;)<A NAME="Index242"></A><A NAME="Index243"></A></B>,
<B>fail(&#160;)<A NAME="Index244"></A><A NAME="Index245"></A></B>, and
<B>bad(&#160;)<A NAME="Index246"></A><A NAME="Index247"></A></B>. These return
state information based on the <B>eofbit<A NAME="Index248"></A></B>
<A NAME="Index249"></A>(indicates the buffer is at the end of sequence), the
<B>failbit<A NAME="Index250"></A></B> <A NAME="Index251"></A>(indicates some
operation has failed because of formatting issues or some other problem that
does not affect the buffer) and the
<B>badbit<A NAME="Index252"></A><A NAME="Index253"></A></B> (indicates something
has gone wrong with the buffer).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">However, as mentioned earlier, the state
of an input stream generally gets corrupted in weird ways only when you&#8217;re
trying to do input to specific types and the type read from the input is
inconsistent with what is expected. Then of course you have the problem of what
to do with the input stream to correct the problem. If you follow my advice and
read input a line at a time or as a big glob of characters (with
<B>read(&#160;)</B>) and don&#8217;t attempt to use the input formatting
functions except in simple cases, then all you&#8217;re concerned with is
whether you&#8217;re at the end of the input (EOF). Fortunately, testing for
this turns out to be simple and can be done inside of conditionals, such as
<B>while(cin)</B> or <B>if(cin)</B>. For now you&#8217;ll have to accept that
when you use an input stream object in this context, the right value is safely,
correctly and magically produced to indicate whether the object has reached the
end of the input. You can also use the Boolean NOT operator <B>!</B>, as in
<B>if(!cin)</B>, to indicate the stream is <I>not</I> OK; that is, you&#8217;ve
probably reached the end of input and should quit trying to read the
stream.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There are times when the stream becomes
not-OK, but you understand this condition and want to go on using it. For
example, if you reach the end of an input file, the <B>eofbit</B> and
<B>failbit</B> are set, so a conditional on that stream object will indicate the
stream is no longer good. However, you may want to continue using the file, by
seeking to an earlier position and reading more data. To correct the condition,
simply call the <B>clear(&#160;)<A NAME="Index254"></A></B> member
function.</FONT><A NAME="fnB11" HREF="#fn11">[11]</A><A NAME="_Toc305628665"></A><A NAME="_Toc312373881"></A><A NAME="_Toc519041949"></A><BR></P></DIV>
<A NAME="Heading117"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H2 ALIGN="LEFT">
File iostreams</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Manipulating files with iostreams is much
easier and safer than using <B>cstdio <A NAME="Index255"></A></B>in C. All you
do to open a file is create an object; the constructor does the work. You
don&#8217;t have to explicitly close a file (although you can, using the
<B>close(&#160;)</B> member function) because the destructor will close it when
the object goes out of scope.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To create a file that defaults to input,
make an <B>ifstream</B> <A NAME="Index256"></A>object. To create one that
defaults to output, make an <B>ofstream</B>
<A NAME="Index257"></A>object.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here&#8217;s an example that shows many
of the features discussed so far. Note the inclusion of <B>&lt;fstream&gt;
<A NAME="Index258"></A></B>to declare the file I/O classes; this also includes
<B>&lt;iostream&gt;<A NAME="Index259"></A></B>.</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C05:Strfile.cpp</font>
<font color=#009900>// Stream I/O with files</font>
<font color=#009900>// The difference between get() &amp; getline()</font>

⌨️ 快捷键说明

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