An understanding of the CIN Function

Source: Internet
Author: User
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 ....

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.