Introduction of IStream class member functions related to input in C + + _c language

Source: Internet
Author: User

EOF function

EOF is the abbreviation for end of file, which means "ending files." Reads data from the input stream, if the end of the file is reached (in the case of a file terminator), the EOF function value is not 0 (true) or 0 (false).

[Example] reads one line character at a time, outputting the non-whitespace characters.

#include <iostream>
using namespace std;
int main ()
{
  char C;
  while (!cin.eof ())//eof () is false to indicate that the file Terminator
  if ((C=cin.get ())!= ') is not encountered (or not) to check that the character being read is a space character
   cout.put (c);
  return 0;
}

The operating conditions are as follows:

C + + is very interesting.↙
c++isveryinteresting.
^z (end)



Peek Function

Peek is the meaning of "observation," the function of peek is to observe the next character. The form of the call is:

  C=cin.peek ();


The return value of the function is the current character that the pointer points to, but it is only observed, and the pointer remains at the current position and does not move back. If the character to be accessed is a file terminator, the function value is EOF (-1).
putback function

Its invocation is in the form of

  Cin.putback (CH);


The function is to return the character Ch previously read from the input stream with a GET or getline function to the input stream and insert it into the current pointer position for later reading.

[Example] the use of PEEK functions and putback functions.

#include <iostream>
using namespace std;
int main ()
{
  char c[20];
  int ch;
  cout<< "Please enter a sentence:" <<endl;
  Cin.getline (c,15, '/');
  cout<< "The" the "the" is: <<c<<endl;
  Ch=cin.peek (); Watch the current character
  cout<< "the next character (ASCII code) is:" <<ch<<endl;
  Cin.putback (C[0]); Insert ' I ' at the point where the pointer
  cin.getline (c,15, '/');
  cout<< "The second part is:" <<c<<endl;
  return 0;
}

The operating conditions are as follows:

Please enter a sentence:
I am a boy./am a Student./↙ the
"I" is:i am a boy.
The next character (ASCII code) is:32 (next character is a space) the
second part is:i am a student

Ignore function

Its invocation is in the form of

  Cin.ignore (n, terminating character)


The function is to skip n characters in the input stream or to end prematurely when the specified terminating character is encountered (several characters, including the terminating character, are skipped). Such as

  Ighore (5, ' a ')//skip a character in the input stream and stop jumping after ' a '


You can also have no parameters or only one parameter. Such as

  Ignore ()//n default value is, terminating word Fummer that EOF


Equivalent

  Ignore (1, EOF)

[Example] skips the characters in the input stream with the Ignore function. First look at the Ignore function:

#include <iostream>
using namespace std;
int main ()
{
  char ch[20];
  Cin.get (ch,20, '/');
  cout<< "The" the "the" is: <<ch<<endl;
  Cin.get (ch,20, '/');
  cout<< "The second part is:" <<ch<<endl;
  return 0;
}

The results of the operation are as follows:

I like c++./i study c++./i am Happy.↙ the
"I" is:i like C + +.
The second part is: (no valid characters are read from the input stream in the character array ch)

If you want the second cin.get function to read "I study C + +.", you should try to skip the first '/' in the input stream, and you can use the Ignore function to do this, and change the program to:

#include <iostream>
using namespace std;
int main ()
{
  char ch[20];
  Cin.get (ch,20, '/');
  cout<< "The" the "the" is: <<ch<<endl;
  Cin.ignore ();//Skip a character
  cin.get (ch,20, '/') in the input stream;
  cout<< "The second part is:" <<ch<<endl;
  return 0;
}

The results of the operation are as follows:

I like c++./i study c++./i am Happy.↙ the
"I" is:i like C + +.
The second part IS:I study C + +.

The member functions described above can be invoked not only with the cin stream object, but also with other stream objects of the IStream class.

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.