Cin.gcount () and its applications

I wrote the following code

#include< iostream>  
using namespace std;  
int main()  
{  
 char a[30];  
 cin.read(a,10);  
 cout<<(cin.gcount());  
  system("pause");  
  return 0;     
}  

the output was 10, as expected .... but then I wrote the following code

#include< iostream>    
using namespace std;   
int main()    
{    
 char a[30];    
 cin>>a;   
 cout<<(cin.gcount());    
  system("pause");    
  return 0;       
}   

I entered a "hello" that was saved in ... the output this time was 0 instead of 5 ... if cin.gcount () returns the number of bytes read in the last input operation, why is this difference

+3
source share
3 answers
Returns the number of characters extracted by the last unformatted input 
operation performed on the object.

The unformatted input operations that modify the value returned by 
this function are those performed by the following member functions: 
get, getline, ignore, peek, read, readsome, putback and unget.
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^

Notice though, that peek, putback and unget do not extract characters. 
So gcount will always return zero after a call to any of these.

: http://www.cplusplus.com/reference/iostream/istream/gcount/

std::cin .

+4

cin.gcount() , get() getline() read()..... , ... ... http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html

+3

, , gcount unformatted. , read .

+2

All Articles