An introduction to the input related member functions of the istream class in C++

  • 2020-05-05 11:33:35
  • OfStack

eof function

eof is short for end of file, meaning "end of file". Reading from the input stream, the eof function value is non-zero (true) if it reaches the end of the file (at the end of the file), and zero (false) otherwise.

Read a line of characters one by one and print out the non-space characters.


#include <iostream>
using namespace std;
int main( )
{
  char c;
  while(!cin.eof( )) //eof( ) Is false to indicate that no file terminator was encountered 
  if((c=cin.get( ))!=' ') // Checks whether the character read in is a space character 
   cout.put(c);
  return 0;
}

Here's how it works :


C++ is very interesting. � 
C++isveryinteresting.
^Z( The end of the )



peek function

peek means "observe," and the peek function is used to observe the next character. The call form is


  c=cin.peek( );


The return value of the function is the current character that the pointer points to, but it only observes that 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

It is called


  cin.putback(ch);


It returns to the input stream the character ch that was previously read from the input stream by the get or getline functions and inserts it into the current pointer position for later reading.

The usage of peek function and putback function.


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

The operation is as follows :


please enter a sentence:
I am a boy./ am a student./ � 
The first part is:I am a boy.
The next character(ASCII code) is:32( The next character is a space )
The second part is:I am a student

ignore function

It is called


  cin.ignore(n,  Termination of the characters )


The n () function skips the n characters in the input stream or terminates prematurely when a specified termination character is encountered (in which case several characters, including the termination character, are skipped). Such as
 


  ighore(5, 'A') // Skip a character in the input stream 'A' Then he stopped jumping 


You can also take no or only one parameter. Such as


  ignore( ) // n The default value is, and the termination character is EOF


The equivalent of


  ignore(1, EOF)

Skip the characters in the input stream with the ignore function. Let's start with the ignore function:


C++ is very interesting. � 
C++isveryinteresting.
^Z( The end of the )
0

The result is as follows:


C++ is very interesting. � 
C++isveryinteresting.
^Z( The end of the )
1

If you want the second cin.get function to read "I study C++.", you should try to skip the first '/' in the input stream


C++ is very interesting. � 
C++isveryinteresting.
^Z( The end of the )
2

The result is:


I like C++./I study C++./I am happy. � 
The first part is:I like C++.
The second part is:I study C++.

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


Related articles: