Use of file Terminator

Source: Internet
Author: User

The Code is as follows:
--------------------------------------------------------------
Int main ()
{
Char ch;
Int count = 0;
Cin. get (ch );
While (cin. fail () = false)
{
Cout <ch;
Count ++;
Cin. get (ch );
}
Cout <"\ n" <count <"characters read \ n ";
Return 0;
}
---------------------------------------------------------
VC6.0 Console mode (^ z simulates the end of a file in VC)
Why does the program not end when I enter a string of characters plus ^ z and press enter ??!!

For example, enter abcd ^ z and press Enter.
Program echo abcd

Justification
When ^ z is read, the end of the file is detected. cin sets both eofbit and failbit to 1.
If eofbit or failbit is set to 1, cin. fail returns true. The program should be terminated. Why not end the program during debugging?

Enter abcd and press Enter.
Program ECHO: abcd Line Break (invisible), and then wait for the input. Press ^ Z to press Enter.

Why is the last situation unavailable?
Confused. Search for answers. Thank you !!


Answer:
Cause analysis:

The input buffer is a row buffer. After entering a string of characters on the keyboard and pressing enter, these characters are first sent to the input buffer for storage. Every time you press the Enter key, cin. get () checks whether readable data exists in the input buffer. Cin. get () also checks whether there are Ctrl + Z or Ctrl + D keys on the keyboard as the stream stop sign. There are two ways to check the result: blocking and non-blocking.

Blocking Check means to check whether Ctrl Z is used only after the Enter key is pressed. Non-blocking mode means to immediately respond after pressing Ctrl D. If you have input characters from the keyboard before pressing Ctrl D, Ctrl D serves as a carriage return, that is, sending these characters to the input buffer for reading, in this case, Ctrl D no longer serves as the stream Terminator. If there is no keyboard input before pressing Ctrl D, Ctrl D is the signal of the stream end.
In Windows systems, blocking checks are generally used for Ctrl Z, Unix/Linux systems, and non-blocking checks for Ctrl D. The main user is in Windows, so the blocked Ctrl Z is used to mark the end of the stream.

This blocking method has one feature: it is possible to detect whether Ctrl Z is pressed before pressing the carriage return. Another feature is that Ctrl Z is not detected if there is readable data in the input buffer (because the data to be read cannot be considered at the end of the stream ). Another thing we need to know: Ctrl Z does not produce a common ASCII code value, that is, it does not produce a character, therefore, it is not stored in the input buffer as other characters entered from the keyboard. After understanding these points, you can explain the questions raised by the landlord.

After entering abcd ^ z on the keyboard and pressing enter, it will be handled in the Windows system as follows: Due to the press ENTER function, the previous abcd and other characters will be sent to the input buffer (Note: As mentioned above, ^ z does not generate characters, so it is not stored in the input buffer, and there is no ^ z in the buffer ). In this case, cin. get () detects that data already exists in the input buffer (so it no longer checks whether ^ z input exists), and reads the corresponding data from the buffer. If all data is read, the input buffer is empty again, and cin. get () waits for the new input. It can be seen that even if ^ z is pressed, the stream will not end because there are other input characters (abcd) before this.

Therefore, the input stream end condition is: ^ z cannot have any character input (except press Enter), otherwise ^ z cannot end the stream.

Another question
If you enter abcd ^ zabcd
Program echo abcd
And wait for the input, that is, the result after ^ Z is no longer displayed.

-----------------------------------------------
If there is readable data in the input buffer, Ctrl Z is not detected (because there is data to be read, it cannot be considered at the end of the stream ). Another thing we need to know: Ctrl Z does not produce a common ASCII code value, that is, it does not produce a character, therefore, it is not stored in the input buffer as other characters entered from the keyboard.

If you enter abcd ^ zabcd
Program echo abcd
And wait for the input, that is, the result after ^ Z is no longer displayed.
Why is it not handled?
In this case, ^ z can be interpreted as the termination of the keyboard input, but not the termination of the stream.
Bytes --------------------------------------------------------------------------------------------

However, I believe that the answer still does not solve my questions. If the program prompts "please enter the first data", enter ^ Z directly. According to the method provided by a friend on the second floor or return0x0, Ctrl Z is detected because no readable data exists in the input buffer. However, the actual situation is that the input ^ Z does not terminate the running of the program, and you still need to enter ^ Z again to stop the program. Why?


Friends on the 4th floor may use VC2003, as hai040 said:

Vc2003, just one click
Before input, current err state is: 0
Please enter the first data
^ Z
The first data is-858993460
After input the first data, current err state is: 3
Press any key to continue

I use VC 6.0 reprint: http://lipingke.blog.hexun.com/40431101_d.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.