[15] Via
体,, nicrosoft @ sunistudio.com (East day production room, East day document)
FAQS in Section [15]:
[15.1] Why should
[15.1] Why should
Because
Printf () is good, SCANF () regardless of whether it may cause errors, it is still valuable, however, for C I / O (translation: I / O, input / output), their functions are very limited. C I / O (using << and >>) is: ") relative to C (using Printf () and scanf ()).
Better type security: Use
[15.2] When typing illegal characters, why is my program enter the dead cycle? [Recently Changed SO It Uses New-Style Headers and The Std :: Syntax (on 7/00). Click Here To Go To The next FAQ in The "chain" of real ".]
For example, suppose you have the following code, read an integer from std :: cin:
#include
A simple way to check the legal input is to move the input request from the While cycle body to the control expression of the While loop, such as:
#include
[TOP | BOTTOM | Previous Section | Next Section]
-------------------------------------------------- ------------------------------
[15.3] How to work with the blameless while (std :: cin >> foo)? [Recently Changed SO It Uses New-Style Headers and The Std :: Syntax (on 7/00). Click Here To Go To The next FAQ in The "chain" of real ".]
"Weird While (std :: cin >> foo) grammar" see the previous FAQ.
(std :: cin >> foo) expression calls the appropriate Operator >> (for example, it calls the left with the std :: istream parameters, if the type is int, and there is an int & Operator >> on the right) . The std :: istream operator >> The function returns the parameters left on the left, here, it returns std :: cin. The next step is noted that the returned std :: istream is in a Boolean context, so the compiler converts std :: istream into a Boolean value.
The compiler calls a member function called std :: istream :: Operator void * () to convert std :: istream into Boolery. It returns a Void * pointer converted into Bur (NULL to become false, and any other pointer is TRUE). Therefore, the compiler generates the call of std :: cin.operator void * (), just like your icon (void *) std :: cin This explicitly enforces type conversion.
If the stream is in good condition, the conversion operator operator void * () returns a non-pointer, and if it is in a failed state, returns NULL. For example, if it is read too many times (that is, it is already in end-of-file), or the actual input to stream is not the legal type of FOO (eg, if foo is an int, and the data is a "x" Character), the stream will enter the failure and the conversion operator returns NULL.
Operator >> Not simply returns a BOOL (or void *) is why Success or failure is to support the "Cascade" syntax: std :: cin >> foo >> bar; operator >> is combined to the left It means that the code as above will be explained as:
(std :: cin >> foo) >> bar; otherwise, if we turn Operator >> to a normal function name, such as readfrom (), will change to such an expression:
Readfrom (ReadFrom (std :: cin, foo), bar); we always calculate expressions from the inside. Because of the left conjunction of Operator>, it becomes the leftmost expression std :: cin >> foo. This expression returns std :: cin (more appropriate, he returns a reference to the left parameter) to the next expression. The next expression also returns (a reference) to std :: cin, but the second reference is ignored because it is the outermost expression of this "expression statement".
[TOP | BOTTOM | Previous Section | Next Section]
-------------------------------------------------- ------------------------------
[15.4] Why is my input processing exceed the end of the file? [Recently Changed SO It Uses New-Style Headers and The Std :: Syntax (on 7/00). Click Here To Go To The next FAQ in The "chain" of real ".]
Because only after trying to exceed the file last, the EOF tag will be set. That is, when reading the last byte from the file, I have not set an EOF tag. For example, assume that the input stream is mapped to the keyboard - in this case, the C library is not possible to predict whether the characters typed in the user are the last character.
For example, the following code will have "exceed 1" error for the counter i:
INT i = 0; while (! std :: cin.eof ()) {// error! (Unreliable) std :: cin >> x; i; // Work with x ...} You do actually need:
INT i = 0; while (std :: cin >> x) {// correct! (Reliable) i; // Work with x ...} [top | bottom | Previous section | Next section]
-------------------------------------------------- ------------------------------
[15.5] Why after my program is in the first loop, will I ignore the input request? [Recently Changed SO It Uses New-Style Headers and The Std :: Syntax (on 7/00). Click Here To Go To The next FAQ in The "chain" of real ".]
Because the number of extractors retain non-numbers behind the input buffer.
If your code looks like this: char name [1000]; int Age; for (;;) {std :: cout << "name:"; std :: cin >> name; std :: cout << " Age: "; std :: cin >> Age;} and you do practical:
FOR (;;: cin >> age; std :: cin >> age; std :: cin >> age; std :: cin >> age; std :: cin >> Age; std :: cin >> (INT_MAX, '/ N'); This line skips non-digital characters.
[TOP | BOTTOM | Previous Section | Next Section]
-------------------------------------------------- ------------------------------
[15.6] How do I provide printing for CLASS FRED? [Recently Changed SO It Uses New-Style Headers and The Std :: Syntax (on 7/00). Click Here To Go To The next FAQ in The "chain" of real ".]
The operator Operator << is provided with an operator overload.
#include
Note that Operator << returns the stream. This allows the output operator to be cascaded.
[TOP | BOTTOM | Previous Section | Next Section]
-------------------------------------------------- ------------------------------
[15.7] But can I always use the printon () method instead of a friend function? [Recently Created (on 7/00) And Fixed A Bug Thanks To Richard Hector (on 4/01). Click Here To Go To The Next FAQ in The "chain" of rencent changes.
Usually people are always willing to use the Printon () method rather than a friend function because they mistakely believe that the friend destroyed the package and / or the friend is poor. These faith are innocent and wrong: proper use, friend can actually enhance the package.
This is not said that the Printon () method is useless. For example, it is useful when providing printing for a complete inheritance level. But if you see a printon () method, it should usually be protected instead of public.
For complete, here "Printon () method" is given. The idea is a member function (often referred to as Printon () to complete the actual print, and then there is an operator << to call the Rinton () method). When it is incorrectly, the Printon () method is public, so the Operator << does not need to be a friend - it becomes a simple top function, ie not a class friend, is not a class member function. This is some sample code:
#include
On maintenance costs, the "top-level function call members" method will not bring any benefits. We assume that the N-line code is to complete the actual print. In the case of using a friend function, the N-line code will directly access the class's private / protected section, which means that someone will need to be scanned by anyone changed the Private / Protected section of the class, the N-line code will need to be scanned and may Modified, this increases maintenance costs. However, using the Printon () method does not change: We still have N-line code to directly access the classes of the Private / Protected section. Therefore, the code from the friend function moves to the member function does not reduce the maintenance cost. There is no reduction. There is no benefit on maintenance costs. (If any, the Printon () method is worse, because you have an extra original function, now there is more line code needs to be maintained) "Top-level function call members" method makes the class more difficult In particular, the programmer is not a class of designers. This method exposes one of the PUBLIC methods that is not desired to be called to the programmer. When the programmer read the PUBLIC method, they will see two methods to do the same thing. Document needs to be like this: "This is not exactly exactly, but don't use this; but should use that." And the usual programmer will say: "Oh? If I shouldn't use it, why is it public?" In fact, the Printon () method is the only reason for public, to avoid authorizing the friend to operator <<, this proposition Some programmers just want to use this class are subtle and difficult to understand. In short, "Top-level function call members" methods have cost, no benefits. Therefore, usually, not a good idea. Note: If the Printon () method is protected or private, the second objection will not be established. Some situations are reasonable, such as providing printing as a class with a complete inheritance level. Also note that when the Printon () method is non-public, Operator << needs to become a friend.
[TOP | BOTTOM | Previous Section | Next Section]
-------------------------------------------------- ------------------------------
[15.8] How to provide input for CLASS FRED? [Recently Changed SO It Uses New-Style Headers and The Std :: Syntax (on 7/00). Click Here To Go To The next FAQ in The "chain" of real ".]
Use the operator overload to provide an operator Operator >> of a friend's right switching. In addition to the parameters, there is no const: "Fred &" instead of "const fred &", other and output operators.
#include
-------------------------------------------------- ------------------------------
[15.9] How to provide printing for the complete inheritance level? [Recently Changed SO It Uses New-Style Headers and The Std :: Syntax (on 7/00). Click Here To Go To The next FAQ in The "chain" of real ".]
Provide a friend's Operator << call a protected virtual function:
Class base {public: friend std :: ostream & operator << (std :: ostream & o, const base); // ... protected: virtual void printon (std :: ostream & o) const;}; inline std :: Ostream & Operator << (std :: ostream & o, const base & b) {b.Printon (o); return o;} class derived: public base {protected: Virtual Void Printon (std :: ostream & o) const;}; final The result is that Operator << is like dynamic binding, even if it is a friend function. This is called "virtual friends".
Pay attention to the derived class to override Printon (std :: ostream &) const. In particular, they do not provide their own Operator <<.
Naturally, if Base is an ABC (Abstract base class), base :: printon (std :: ostream &) can be declared using the "= 0" syntax as a pure virtual function.
[TOP | BOTTOM | Previous Section | Next Section]
-------------------------------------------------- ------------------------------
[15.10] How to "heavy open" std :: cin and std :: cout in the DOS and / or OS / 2 environment? [Recently Changed So It Uses New-Style Headers and The Std :: Syntax (on 7/00). Click Here To Go To The next FAQ in The "chain" of rencent changes.] This depends on the implementation, please see your The documentation of the compiler.
For example, suppose you want to use std :: cin and std :: cout for binary I / O. Human your operating system (such as DOS or OS / 2) insists that "/ R / N" entered from std :: cin is translated into "/ N", which will output from std :: cout or std :: CERR " / N "translated into" / r / n ".
It is not a standard method that makes std :: cin, std :: cout and / or std :: CERR are opened in binary mode. Close the stream and try to turn them in binary mode, which may get undesired or undesirable results.
At the system's distinction, implementation may provide a way to make them binary stream, but you must see the manual to find.
[TOP | BOTTOM | Previous Section | Next Section]
-------------------------------------------------- ------------------------------
[15.11] Why can't I open a file in different directories such as "../test.dat"? [Recently Changed SO IT Uses New-Style Headers and Usees The Std :: Syntax (on 7/00). Click Here to Go To The Next FAQ in The "chain" of rencent changes.
Because "/ t" is a TAB character.
You should use a forward slash in the file, even in the operating system using a backslash, such as DOS, Windows, OS / 2, etc. E.g:
#include
[TOP | BOTTOM | Previous Section | Next Section]
-------------------------------------------------- ------------------------------
[15.12] How to convert a value (such as, a number) to std :: string? [Recently created Thanks to Rob Stewart (on 7/00). Click Here to Go To The next FAQ in The "Chain" of Recent Changes .] There are two ways: you can use the
#include
In this example, we insert X into O by an overloaded insertion operator <<. It calls the formatting tool for iostream to convert X to a std :: string. IF test guarantees the correct work - for built-in / inherent type, always succeeded, but IF test is a good style.
Expression OS.STR () returns std :: string of anything that is inserted into the stream O, here, is a string of the value of X.
[TOP | BOTTOM | Previous Section | Next Section]
-------------------------------------------------- ------------------------------
[15.13] How to convert std :: string into values? [Recently Created Thanks to Rob Stewart (on 7/00). Click Here to Go To The Next FAQ in The "chain" of real channel.]
There are two ways: you can use the
#include
The IF test guarantees that the conversion works correctly. For example, if the string contains characters that are not suitable for X-type, the IF test will fail.
[TOP | BOTTOM | Previous Section | Next Section]
E-mail the author [C FAQ Lite | Table of Contents | Subject Index | About The Author | © | Download Your Own Copy] Revised Apr 8, 2001