开发者

Using fstream tellg to read a portion of the stream till the end

开发者 https://www.devze.com 2022-12-16 05:11 出处:网络
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

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开发者_Go百科 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.


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


http://groups.google.com/group/comp.lang.c++/browse_thread/thread/709cde3942e64d6c#

0

精彩评论

暂无评论...
验证码 换一张
取 消