Problem
Recently I encountered such a problem in C ++, while (CIN> S) {}, which of the following types of input can be used to end the running of such a statement, the answer is Ctrl + Z.ProgramEnd. However, there is no profound analysis of the role of Ctrl + Z and the essence of Ctrl + Z. As a result, I found some content and thought I had solved the problem. The learned content is recorded as follows. First, let's take a look at the conditions for stopping an input stream.
Stream status
The stream has a normal status and an error status. The stream can work normally only when it is normal. The input stream can be read only when it is working normally. CTRL + Z is used to change the stream from normal working state to non-working state, which can also be said to be a wrong working state, so that while (CIN> S) can be made).
Each stream object maintains a set of condition marks that can be used to monitor the current status of the stream. You can call the following four predicates:
1. If a stream encounters a file Terminator, EOF returns true.
If (inout. EOF ())
// OK, read all
2. If an invalid operation is attempted, for example, the seeking relocation operation exceeds the end of the file. Then, bad () returns true. Generally, this indicates that the stream is damaged due to an undefined method.
3. If the operation fails, if a file stream object fails to be opened or an invalid input format is encountered, fail () returns true.
Ifstream ifile (filename, ios_base: In );
If (ifile. Fail () // cannot open
Error_message (........);
4. if none of the other conditions is true, good () returns true.
If (inout. Good ())
You can modify the condition status of a stream object in either of the following ways. First, you can use the clear () member function to reset the condition state to an explicit value. Second, use the setstate () member function.
Test
Int main (){
Cout <CIN <Endl;
String S;
While (CIN> S)
{
Cout <s;
}
Cout <CIN <Endl;
}
Enter a enter B enter Ctrl + z enter
Running result:
00480f20
A B
00000000
What is Ctrl + Z?
CTRL + Z is used to end the stream, which is equivalent to EOF.
Summary
If (! CIN)
The stream cannot work normally.
Else
Stream works properly
Note This when writing a program. CTRL + Z is just an episode, but I am glad to learn something from it ....