The C++ std::istream::gcount() function is used to return the number of chracters extracted by the last unformatted input operation performed on the stream. It is used after functions like read() to determine how many characters were actually read.
SyntaxThe gcount() function does not modify the streams state and returns a value of type std::streamsize
Following is the syntax for std::istream::gcount() function.
streamsize gcount() const;Parameters
It does not accept any parameters.
Return ValueThis function returns the number of characters extracted by the last unformatted input operation performed on the object.
ExceptionsIf an exception is thrown, there are no changes in the stream.
Data racesAccesses the stream object.
ExampleLet's look at the following example, where we are going to read the characters.
#include <iostream> int main() { std::cout << "Enter Characters: "; char x[5]; std::cin.get(x, 4); std::cout << "Characters read: " << std::cin.gcount() << std::endl; std::cout << "Buffer Contents: " << x << std::endl; return 0; }Output
Output of the above code is as follows −
Enter Characters: abcdefgh Characters read: 3 Buffer Contents: abcExample
Consider the following example, where we are going to read the integers as string.
#include <iostream> int main() { std::cout << "Enter an Integer: "; char buffer[5]; std::cin.getline(buffer, 3); std::cout << "Characters read: " << std::cin.gcount() << std::endl; std::cout << "Buffer Contents: " << buffer << std::endl; return 0; }Output
Following is the output of the above code −
Enter an Integer: 123421 Characters read: 2 Buffer Contents: 12
istream.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4