2

The way of getting the size of a stream that I'm familiar with involves doing:

foo.seekg(0, ios::end); int bar = tellg(); foo.seekg(0, ios::beg); 

But I'd rather not move the stream's position! It obviously already actually knows the start and endpoints of it's buffer, so why can't I just directly access that information?

Somewhere in the recesses of my memory I think there is a way to get this from the stream's ios or ios_base. But I just can't remember. Can someone else recall how to do this?

2
  • 2
    What makes you think it knows the true start/end points of the underlying data? What kind of stream is foo exactly? If it is a file stream, do you expect it to have the entire file buffered in memory at one time? Depending on the stream type, a seek is required. Commented Jul 28, 2015 at 23:57
  • 1
    'Stream size' is already a rubbery concept. What's the size of a stream connected to a socket that the peer hasn't closed yet? A stream is not an array. Commented Jul 29, 2015 at 0:40

2 Answers 2

3

What you're probably referring to is foo.rdbuf()->in_avail(), but you have made the incorrect assumption that a stream always holds its entire contents in the buffer, which isn't going to be the case for things like file streams.

GCC for instance uses a buffer of length BUFSIZ which will be at least 256 bytes (it's 1024 on my system), so any file larger than that cannot be held entirely in a basic_filebuf's buffer.

There's no portable way to get the true size of a stream, for instance a stream opened in text mode on windows won't give you the actual number of bytes in the stream because of EOL conversion. Streams are designed to be read from until you hit the end, depending on what you're trying to do with the stream there may be a better way.

Source-wise any introductory C++ book will tell you as much, but here's the relevant wording in the standard

[streambuf.reqts]

Each sequence is characterized by three pointers which, if non-null, all point into the same charT array object. The array object represents, at any moment, a (sub)sequence of characters from the sequence.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, add just as I post that answer :) Could you post a source for rdbuf not containing the entire buffer?
-2

Took the time to review my memory and it came back to me:

bar = foo.rdbuf()->in_avail(); 

You can see a live example here: http://coliru.stacked-crooked.com/a/66b6182627aebbe6


Contrary to this comment this will work for any C++ stream because they all inherit from ios which provides rdbuf.

enter image description here

1 Comment

This doesn't get the size of the stream. It does get the size of what is in the read buffer, which may or may not be the size of the stream, if the stream can even be said to have a concept of size. What about network or compressed file streams?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.