|
After searching for a long time on the Internet, I finally found the answer, thank you whyglinux (山青水秀).
Here is the original text:
The reasons are as follows:
The input buffer is a line buffer. When you enter a string of characters from the keyboard and press Enter, these characters are first sent to the input buffer for storage. Whenever the enter key is pressed, cin.get () detects if there is readable data in the input buffer. cin.get () will also check if there is a Ctrl + Z or Ctrl + D key on the keyboard as the end of stream sign. There are two ways to check: blocking and non-blocking.
The blocking check method refers to checking whether there is a Ctrl + Z key combination pressed before pressing the Enter key. The non-blocking check method refers to a method of responding immediately after pressing Ctrl + D. If characters have been entered from the keyboard before pressing Ctrl + D, the effect of Ctrl + D is equivalent to a carriage return, that is, these characters are sent to the input buffer for reading. At this time, Ctrl + D no longer serves as the end-of-stream character. Role. If there is no keyboard input before pressing Ctrl + D, Ctrl + D is the signal for the end of the stream.
Windows systems generally use blocking Ctrl + Z, and Unix / Linux systems generally use non-blocking checking Ctrl + D. The host is under Windows, so use blocking Ctrl + Z to mark the end of the stream.
This blocking method has a feature: it is only possible to detect if there was a Ctrl + Z press before pressing Enter. Another feature is that if there is readable data in the input buffer, Ctrl + Z will not be detected (because there is data to be read, the end of the stream cannot be considered yet). One more thing to know: Ctrl + Z does not produce an ordinary ASCII value, which means that it does not produce a character, so it cannot be stored in the input buffer like other characters entered from the keyboard. After understanding these points, we can explain the questions raised by the landlord.
After entering abcd ^ z from the keyboard and entering the carriage return, it is processed as follows on the Windows system: due to the effect of the carriage return, the characters such as abcd are sent to the input buffer (note: as mentioned above, ^ z will not produce Characters, so it will not be stored in the input buffer, there is no ^ z in the buffer). At this point, cin.get () detects that there is already data in the input buffer (so it no longer checks for ^ z input) and reads the corresponding data from the buffer. If they are all read, the input buffer becomes empty again, and cin.get () waits for new input. It can be seen that although there is ^ z pressed, because there are other input characters (abcd) before this, the stream will not end.
Therefore, the condition for the end of the input stream is that no characters can be entered before the ^ z (except carriage return), otherwise ^ z will not play the role of the end of the stream.
I will post it in half an hour. I hope you all have a look at this question! |
|