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

📄 ch16.htm

📁 vc的电子书
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<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 other
devices. Standard error is often redirected to a file, and standard input and output
can be piped to files using operating system commands.</P>

<DL>
	<DD>
<HR>
<FONT COLOR="#000077"><B>New Term: </B></FONT><I>Redirecting</I> refers to sending
	output (or input) to a place different than the default. The redirection operators
	for DOS and UNIX are (<TT>&lt;</TT>) redirect input and (<TT>&gt;</TT>) redirect
	output. 
<HR>

</DL>

<P>Piping refers to using the output of one program as the input of another.</P>
<P>DOS provides rudimentary redirection commands, such as redirect output (<TT>&gt;</TT>)
and (<TT>&gt;</TT>)redirect input (<TT>&lt;</TT>). UNIX provides more advanced redirection
capabilities, but the general idea is the same: Take the output intended for the
screen and write it to a file, or pipe it into another program. Alternatively, the
input for a program can be extracted from a file rather than from the keyboard.</P>
<P>Redirection is more a function of the operating system than of the <TT>iostream</TT>
libraries. C++ just provides access to the four standard devices; it is up to the
user to redirect the devices to whatever alternatives are needed.
<H3 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">Input Using cin</FONT></H3>
<P>The global object <TT>cin</TT> is responsible for input and is made available
to your program when you include <TT>iostream.h</TT>. In previous examples, you used
the overloaded extraction operator (<TT>&gt;&gt;</TT>) to put data into your program's
variables. How does this work? The syntax, as you may remember, is the following:</P>
<PRE><FONT COLOR="#0066FF">int someVariable;
cout &lt;&lt; &quot;Enter a number: &quot;;
cin &gt;&gt; someVariable;
</FONT></PRE>
<P>The global object <TT>cout</TT> is discussed later today; for now, focus on the
third line, <TT>cin &gt;&gt; someVariable;</TT>. What can you guess about <TT>cin</TT>?</P>
<P>Clearly it must be a global object, because you didn't define it in your own code.
You know from previous operator experience that <TT>cin</TT> has overloaded the extraction
operator (<TT>&gt;&gt;</TT>) and that the effect is to write whatever data <TT>cin</TT>
has in its buffer into your local variable, <TT>someVariable</TT>.</P>
<P>What may not be immediately obvious is that <TT>cin</TT> has overloaded the extraction
operator for a great variety of parameters, among them <TT>int&amp;</TT>, <TT>short&amp;</TT>,
<TT>long&amp;</TT>, <TT>double&amp;</TT>, <TT>float&amp;</TT>, <TT>char&amp;</TT>,
<TT>char*</TT>, and so forth. When you write <TT>cin &gt;&gt; someVariable;</TT>,
the type of <TT>someVariable</TT> is assessed. In the example above, <TT>someVariable</TT>
is an integer, so the following function is called:</P>
<PRE><FONT COLOR="#0066FF">istream &amp; operator&gt;&gt; (int &amp;)
</FONT></PRE>
<P>Note that because the parameter is passed by reference, the extraction operator
is able to act on the original variable. Listing 16.1 illustrates the use of <TT>cin</TT>.</P>
<P><A NAME="Heading15"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.1. cin handles
different data types.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1:     //Listing 16.1 -- character strings and cin
2:
3:     #include &lt;iostream.h&gt;
4:
5:     int main()
6:     {
7:        int myInt;
8:        long myLong;
9:        double myDouble;
10:       float myFloat;
11:       unsigned int myUnsigned;
12:
13:       cout &lt;&lt; &quot;int: &quot;;
14:       cin &gt;&gt; myInt;
15:       cout &lt;&lt; &quot;Long: &quot;;
16:       cin &gt;&gt; myLong;
17:       cout &lt;&lt; &quot;Double: &quot;;
18:       cin &gt;&gt; myDouble;
19:       cout &lt;&lt; &quot;Float: &quot;;
20:       cin &gt;&gt; myFloat;
21:       cout &lt;&lt; &quot;Unsigned: &quot;;
22:       cin &gt;&gt; myUnsigned;
23:
24:       cout &lt;&lt; &quot;\n\nInt:\t&quot; &lt;&lt; myInt &lt;&lt; endl;
25:       cout &lt;&lt; &quot;Long:\t&quot; &lt;&lt; myLong &lt;&lt; endl;
26:       cout &lt;&lt; &quot;Double:\t&quot; &lt;&lt; myDouble &lt;&lt; endl;
27:       cout &lt;&lt; &quot;Float:\t&quot; &lt;&lt; myFloat &lt;&lt; endl;
28:       cout &lt;&lt; &quot;Unsigned:\t&quot; &lt;&lt; myUnsigned &lt;&lt; endl;
29:     return 0;
<TT>30: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: int: 2
Long: 70000
Double: 987654321
Float: 3.33
Unsigned: 25

Int:    2
Long:   70000
Double: 9.87654e+08
Float:  3.33
Unsigned:       25
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis</B></FONT><FONT COLOR="#000000"><B>: </B></FONT>On
lines 7-11, variables of various types are declared. On lines 13-22, the user is
prompted to enter values for these variables, and the results are printed (using
<TT>cout</TT>) on lines 24-28.</P>
<P>The output reflects that the variables were put into the right &quot;kinds&quot;
of variables, and the program works as you might expect.
<H4 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">Strings</FONT></H4>
<P><TT>cin</TT> can also handle character pointer (<TT>char*</TT>) arguments; thus,
you can create a character buffer and use <TT>cin</TT> to fill it. For example, you
can write this:</P>
<PRE><FONT COLOR="#0066FF">char YourName[50]
cout &lt;&lt; &quot;Enter your name: &quot;;
cin &gt;&gt; YourName;
</FONT></PRE>
<P>If you enter <TT>Jesse</TT>, the variable <TT>YourName</TT> will be filled with
the characters <TT>J, e, s, s, e, \0</TT>. The last character is a null; <TT>cin</TT>
automatically ends the string with a null character, and you must have enough room
in the buffer to allow for the entire string plus the null. The null signals &quot;end
of string&quot; to the standard library functions discussed on Day 21, &quot;What's
Next.&quot;
<H4 ALIGN="CENTER"><A NAME="Heading18"></A><FONT COLOR="#000077">String Problems</FONT></H4>
<P>After all this success with <TT>cin</TT>, you might be surprised when you try
to enter a full name into a string. <TT>cin</TT> believes that white space is a separator.
When it sees a space or a new line, it assumes the input for the parameter is complete,
and in the case of strings it adds a null character right then and there. Listing
16.2 illustrates this problem.</P>
<P><A NAME="Heading19"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.2. Trying
to write more than one word to cin.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1:     //Listing 16.2 -- character strings and cin
2:
3:     #include &lt;iostream.h&gt;
4:
5:     int main()
6:     {
7:        char YourName[50];
8:        cout &lt;&lt; &quot;Your first name: &quot;;
9:        cin &gt;&gt; YourName;
10:       cout &lt;&lt; &quot;Here it is: &quot; &lt;&lt; YourName &lt;&lt; endl;
11:       cout &lt;&lt; &quot;Your entire name: &quot;;
12:       cin &gt;&gt; YourName;
13:       cout &lt;&lt; &quot;Here it is: &quot; &lt;&lt; YourName &lt;&lt; endl;
14:     return 0;
<TT>15: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Your first name: Jesse
Here it is: Jesse
Your entire name: Jesse Liberty
Here it is: Jesse
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis</B></FONT><B>: </B>On line 7, a character array
is created to hold the user's input. On line 8, the user is prompted to enter one
name, and that name is stored properly, as shown in the output.</P>
<P>On line 11, the user is again prompted, this time for a full name. <TT>cin</TT>
reads the input, and when it sees the space between the names, it puts a null character
after the first word and terminates input. This is not exactly what was intended.</P>
<P>To understand why this works this way, examine Listing 16.3, which shows input
for a number of fields.</P>
<P><A NAME="Heading21"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.3. Multiple
input.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1:     //Listing 16.3 - character strings and cin
2:
3:     #include &lt;iostream.h&gt;
4:
5:     int main()
6:     {
7:        int myInt;
8:        long myLong;
9:        double myDouble;
10:       float myFloat;
11:       unsigned int myUnsigned;
12:       char myWord[50];
13:
14:       cout &lt;&lt; &quot;int: &quot;;
15:       cin &gt;&gt; myInt;
16:       cout &lt;&lt; &quot;Long: &quot;;
17:       cin &gt;&gt; myLong;
18:       cout &lt;&lt; &quot;Double: &quot;;
19:       cin &gt;&gt; myDouble;
20:       cout &lt;&lt; &quot;Float: &quot;;
21:       cin &gt;&gt; myFloat;
22:       cout &lt;&lt; &quot;Word: &quot;;
23:       cin &gt;&gt; myWord;
24:       cout &lt;&lt; &quot;Unsigned: &quot;;
25:       cin &gt;&gt; myUnsigned;
26:
27:       cout &lt;&lt; &quot;\n\nInt:\t&quot; &lt;&lt; myInt &lt;&lt; endl;
28:       cout &lt;&lt; &quot;Long:\t&quot; &lt;&lt; myLong &lt;&lt; endl;
29:       cout &lt;&lt; &quot;Double:\t&quot; &lt;&lt; myDouble &lt;&lt; endl;
30:       cout &lt;&lt; &quot;Float:\t&quot; &lt;&lt; myFloat &lt;&lt; endl;
31:       cout &lt;&lt; &quot;Word: \t&quot; &lt;&lt; myWord &lt;&lt; endl;
32:       cout &lt;&lt; &quot;Unsigned:\t&quot; &lt;&lt; myUnsigned &lt;&lt; endl;
33:
34:       cout &lt;&lt; &quot;\n\nInt, Long, Double, Float, Word, Unsigned: &quot;;
35:       cin &gt;&gt; myInt &gt;&gt; myLong &gt;&gt; myDouble;
36:       cin &gt;&gt; myFloat &gt;&gt; myWord &gt;&gt; myUnsigned;
37:       cout &lt;&lt; &quot;\n\nInt:\t&quot; &lt;&lt; myInt &lt;&lt; endl;
38:       cout &lt;&lt; &quot;Long:\t&quot; &lt;&lt; myLong &lt;&lt; endl;
39:       cout &lt;&lt; &quot;Double:\t&quot; &lt;&lt; myDouble &lt;&lt; endl;
40:       cout &lt;&lt; &quot;Float:\t&quot; &lt;&lt; myFloat &lt;&lt; endl;
41:       cout &lt;&lt; &quot;Word: \t&quot; &lt;&lt; myWord &lt;&lt; endl;
42:       cout &lt;&lt; &quot;Unsigned:\t&quot; &lt;&lt; myUnsigned &lt;&lt; endl;
43:
44:
45:     return 0;
<TT>46: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Int: 2
Long: 30303
Double: 393939397834
Float: 3.33
Word: Hello
Unsigned: 85

Int:    2
Long:   30303
Double: 3.93939e+11
Float:  3.33
Word:   Hello
Unsigned:       85

Int, Long, Double, Float, Word, Unsigned: 3 304938 393847473 6.66 bye -2

Int:    3
Long:   304938
Double: 3.93847e+08
Float:  6.66
Word:   bye
</FONT></PRE>
<PRE><FONT COLOR="#0066FF">
Unsigned:       65534
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis</B></FONT><FONT COLOR="#000000"><B>:</B></FONT><B>
</B>Once again, a number of variables are created, this time including a <TT>char</TT>
array. The user is prompted for input and the output is faithfully printed.</P>
<P>On line 34, the user is prompted for all the input at once, and then each &quot;word&quot;
of input is assigned to the appropriate variable. It is in order to facilitate this
kind of multiple assignment that <TT>cin</TT> must consider each word in the input
to be the full input for each variable. If <TT>cin</TT> was to consider the entire
input to be part of one variable's input, this kind of concatenated input would be
impossible.</P>
<P>Note that on line 35 the last object requested was an <TT>unsigned</TT> integer,
but the user entered <TT>-2</TT>. Because <TT>cin</TT> believes it is writing to
an <TT>unsigned</TT> integer, the bit pattern of <TT>-2</TT> was evaluated as an
<TT>unsigned</TT> integer, and when written out by <TT>cout</TT>, the value <TT>65534</TT>
was displayed. The <TT>unsigned</TT> value <TT>65534</TT> has the exact bit pattern
of the <TT>signed</TT> value <TT>-2</TT>.</P>
<P>Later in this chapter you will see how to enter an entire string into a buffer,
including multiple words. For now, the question arises, &quot;How does the extraction
operator manage this trick of concatenation?&quot;
<H4 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">operator&gt;&gt;
Returns a Reference to an istream Object</FONT></H4>
<P>The return value of <TT>cin</TT> is a reference to an <TT>istream</TT> object.
Because <TT>cin</TT> itself is an <TT>istream</TT> object, the return value of one
extraction operation can be the input to the next extraction.</P>
<PRE><FONT COLOR="#0066FF">int VarOne, varTwo, varThree;
cout &lt;&lt; &quot;Enter three numbers: &quot;
cin &gt;&gt; VarOne &gt;&gt; varTwo &gt;&gt; varThree;
</FONT></PRE>
<P>When you write <TT>cin &gt;&gt; VarOne &gt;&gt; varTwo &gt;&gt; varThree;</TT>,
the first extraction is evaluated <TT>(cin &gt;&gt; VarOne)</TT>. The return value
from this is another <TT>istream</TT> object, and that object's extraction operator
gets the variable <TT>varTwo</TT>. It is as if you had written this:</P>
<PRE><FONT COLOR="#0066FF">((cin &gt;&gt; varOne) &gt;&gt; varTwo) &gt;&gt; varThree;
</FONT></PRE>
<P>You'll see this technique repeated later when <TT>cout</TT> is discussed.
<H3 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">Other Member Functions
of cin</FONT></H3>
<P>In addition to overloading <TT>operator&gt;&gt;</TT>, <TT>cin</TT> has a number
of other member functions. These are used when finer control over the input is required.

⌨️ 快捷键说明

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