3
#include <fstream>
#include <iostream>


int main(int argc, char * argv [])
{
    std::ifstream f{ "test.syp" };
    std::cout << f.tellg() << '\n';
    char buffer[4];
    f.read(buffer, 4);
    std::cout << f.gcount() << '\n';
    std::cout << f.tellg() << '\n';
}

When I execute the above code, I get the following output:

0
4
20

If I change ifstream to fstream, I get the same thing except that the last number is 21.

I would expect the last number to be 4 in both instances. Why isn't it?

Edit: I get the expected result if I open the file with std::ios::binary; it must be a quirk of text-mode

Sydius
  • 13,567
  • 17
  • 59
  • 76

1 Answers1

0

f.gcount returns the number of characters read by the last unformatted input operation (in this case, f.read()).

f.tellg() returns an input position indicator; that's an opaque value that has meaning to seekg(), but its numeric value isn't meaningful to user programs.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103