0

I'm trying to get file size value from range-v3, like this.

std::ifstream i("test.bin", std::ios::binary | std::ios::in);
auto rng = ranges::istream_range<unsigned char>(i);
std::cout << ranges::distance(rng);

However it seems that a wrong size is returned from the distance function. Why?

godbolt.org/g/DsouJE

sandthorn
  • 2,770
  • 1
  • 15
  • 59
  • If you think about it a little, it makes sense that it *can't* give you the size. It needs to *read the whole file* to be able to give you its size, and if the file is large that really doesn't make any sense. – Some programmer dude Apr 29 '18 at 14:20

1 Answers1

1

istream_range uses operator>> to read elements. operator>> is a formatted input operator, it skips whitespace, may convert line endings etc. That is why you get wrong results.

And even this method would give correct results, it is very inefficient. It reads the whole file.

Ilya Popov
  • 3,765
  • 1
  • 17
  • 30