Use of File Terminator

Source: Internet
Author: User

Problem Description: Under Windows
int main() 
{
String str; 
while (cin>>str);    
cout<<"\ n"<<"End"<<endl;      
}
When the console enters multiple strings, the input file Terminator crtl-z does not end the while loop, how do I jump out of the while loop?

Workaround:
Method 2: Change the code to set your own file terminator
String A;
while ((cin>> a) &&a!= ' X ')
X the file terminator that you can set for yourself.
Cause resolution:
When you enter a string of characters from the keyboard and press ENTER, the characters inode are first sent to the input buffer for storage. Whenever the ENTER key is pressed, cin.get () detects if the input buffer has readable data. Cin.get () also checks for ctrl-z or ctrl-d keys on the keyboard for flow-end identification, which are checked in two ways, blocking and non-blocking.
The Windows system generally uses the blocking check Ctrl-z,unix/linux system generally adopts the non-blocking check ctrl-d.
The blocking check method refers to whether or not the CTRL-Z key combination is pressed before the return enter is pressed. In addition, if there is readable data in the input buffer, CTRL-Z is not detected because there is readable data, and the description is not yet at the end of the stream. Note that ctrl-z produces not a normal ASCII character, so ctrl-z will not be stored in the input buffer as other characters entered from the keyboard. Therefore, shut up ctrl-z, the input ctrl-z should not be entered before any characters, that is, enter the input key before all the characters entered into the buffer, followed by the input ctrl-z, and then enter the return, tell the system detection flow Terminator ctrl-z.
Non-blocking is the way to respond immediately after pressing CTRL-D. If a character has been entered from the keyboard before the ctrl-d input, ctrl-d is equivalent to carriage return, which sends the characters to the input buffer for reading, at which point the ctrl+d does not function as a stream terminator. If ctrl-d input value does not have any keyboard input, then ctrl-d is the signal of the end of the stream.
Examples of Windows ctrl-z use are as follows:
After entering abcd^z from the keyboard and entering the ENTER key, the system is processed as follows: Because of the function of the carriage return, the preceding ABCD characters are sent to the input buffer, and the ^z does not generate ego, so it is not stored in the buffer. This is, Cin.get () detects that there is already data in the input buffer, does not detect whether there is ^z presence, cin.get () reads the corresponding data from the buffer, if it is finished, the input buffer re-edge is empty, cin.get () waits for a new input, visible despite the ctrl-z input , but before there are other characters, the system does not monitor the stream terminator.
Therefore, the condition of the input stream terminator is: Ctrl-z can not have any character input (except enter), and then use the Enter to notify the system to do stream Terminator detection.

Zhuandi_h NetEase Blog C + + file conversion character, original address: http://blog.163.com/zhuandi_h/blog/static/1802702882012210114352789/

Use of File Terminator

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.