C++ std::istream::gcount() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, gcount() tells you how many characters the last unformatted input function extracted from the stream. Unformatted functions are get(), getline(), read(), readsome(), and ignore().

It is most useful with read(), because a short read at the end of a file leaves you unsure how many bytes really arrived. Include <iostream> to use it.

This is a member function of std::istream. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.

Syntax

Call gcount right after an unformatted input call, before doing another read.

C++
in.read(buf, 10);
std::streamsize n = in.gcount();     // how many arrived
CallWhat it does
in.gcount()Returns the count of the last unformatted read.
in.read(buf, 10); in.gcount()Tells you how many bytes arrived, which can be less than 10.
in.getline(buf, 50); in.gcount()Includes the new line, even though it is not stored in buf.

Formatted reads such as in >> x do not change the count. gcount() keeps the value from the last unformatted call.

Library declarations
C++
streamsize gcount() const;

Parameters

ParameterDescription
Nonegcount() takes no parameters.

Return Value

Return ValueDescription
std::streamsizeThe number of characters extracted by the last unformatted input call. It is 0 if no such call has been made.

C++ Examples

Example 1 — gcount() After getline()

The count includes the new line that getline() removed.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("hello\nworld\n");
    char buf[20];

    in.getline(buf, 20);

    std::cout << "text: " << buf << '\n';
    std::cout << "gcount: " << in.gcount() << '\n';
    return 0;
}

How It Works

  1. getline() stores hello in buf. That is 5 characters.
  2. It also took the new line out of the stream. The new line is not stored, but it is counted.
  3. So gcount() prints 6: five letters plus one new line.

Example 2 — Find Out How Much read() Got

Ask for 10 characters when only 3 exist. The count tells the truth.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("abc");
    char buf[10];

    in.read(buf, 10);

    std::cout << "asked for 10\n";
    std::cout << "got: " << in.gcount() << '\n';
    std::cout << "fail: " << (in.fail() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. read(buf, 10) tries to take 10 characters, but the text has only three.
  2. It stores abc, hits the end, and sets eofbit and failbit.
  3. gcount() reports 3, so you know that only the first three places of buf hold real data.

Example 3 — gcount() Ignores Formatted Input

The count changes after get() but not after >>.

C++
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::istringstream in("hello world");
    char c;
    std::string word;

    in.get(c);
    std::cout << "after get: " << in.gcount() << '\n';

    in >> word;
    std::cout << "after >>:  " << in.gcount() << '\n';
    std::cout << "word: " << word << '\n';
    return 0;
}

How It Works

  1. in.get(c) takes the letter h, so gcount() is 1.
  2. in >> word is a formatted read. It reads ello but does not touch the count, which stays 1.
  3. If you need the length of a word read with >>, use word.size() instead.

Frequently Asked Questions

It returns how many characters the last unformatted input call (get, getline, read, readsome, ignore) took from the stream.
Yes. getline() removes the new line, and it counts as one extracted character, even though it is not stored.
No. Formatted input does not update the count. gcount() keeps the value of the last unformatted call.
After read(). A read near the end of a file can return fewer bytes than you asked for, and gcount() tells you exactly how many.
It returns std::streamsize, a signed integer type that is large enough for any stream size.

Next: get()

Read one character, spaces and new lines included.

get() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful