views:

168

answers:

2

Hello:

I have this simple code that needs to get a chunk of a large log file that is being written into. At some point it stores the current location returned from streampos start = istream::tellg(); method. Later on the code has to read from the stream a buffer from the start till the end. The code is approximately like this:

streampos start = my_stream.tellg();

... // do some stuff with logging

streampos end = my_stream.tellg();
const streamsize size_to_read = (end - start);
char *buf = new char[size_to_read];

lock (m_logReadLock);
{
    my_stream.flush();
    my_stream.seekg(start);
    my_stream.read(buf, size_to_read);
    size_read = my_stream->gcount();
}
unlock (m_logReadLock);

The effect that I'm observing is that size_read is smaller than size_to_read and the stream has its eof flag set. Shouldn't the end pointer specify exactly where the stream ends and read() method return that exact amount of data? It is fine, I can work round it by checking the eof flag. However, can anyone provide the explanation for this effect?

Thanks.

+1  A: 

You seem to be calling gcount on stream_loc instead of my_stream.

interjay
Sorry, copy paste error.
ilya1725
Is using `begin_stream_pos`, `begin`, and `start` for the same thing another copy paste error?
interjay
Yes,thanks for noticing.
ilya1725