c++ - How can I find out how many bytes are available from a std::istream? -
c++ - How can I find out how many bytes are available from a std::istream? -
if wanted read()
content of std::istream
in buffer, have find out how much info available first know how big create buffer. , number of available bytes istream, doing this:
std::streamsize available( std::istream &is ) { std::streampos pos = is.tellg(); is.seekg( 0, std::ios::end ); std::streamsize len = is.tellg() - pos; is.seekg( pos ); homecoming len; }
and similarly, since std::istream::eof() isn't useful fundtion afaict, find out if istream
's pointer @ end of stream, i'm doing this:
bool at_eof( std::istream &is ) { homecoming available( ) == 0; }
my question:
is there improve way of getting number of available bytes istream
? if not in standard library, in boost, perhaps?
for std::cin
don't need worry buffering because buffered --- , can't predict how many keys user strokes.
for opened binary std::ifstream
, buffered, can phone call seekg(0, std::ios:end)
, tellg()
methods determine, how many bytes there.
you can phone call gcount()
method after reading:
char buffer[size]; while (in.read(buffer,size)) { std::streamsize num = in.gcount(); // phone call api num bytes in buffer }
for text input reading via std::getline(inputstream, a_string)
, analyzing string afterwards can useful.
c++ boost stl istream
Comments
Post a Comment