This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

An Old Problem?


My current problem *may* have been partially addressed in the post
below, but I kinda think something may have been missed...The problem
below was regarding one extra superfluous character after a stream
read...

http://sourceware.cygnus.com/ml/libstdc++/1999-q2/msg00294.html

My System:
System: RH6.1 on intel i686
GCC 2.95.2
Libstdc++  2.90.7


This problem has to do with the relationship between file size and the
size of the ReadBufferSize I use as an argument to ifstream::read().

I find that if the filesize of the file I am reading is exactly some
integer multiple 'n' times the size of my ReadBufferSize minus 1, then
ifstream::read()  gives me an extra character, and the retval it gives
me includes this extra character in its count.

Real world example: 
	Size of file to be read: 2599 bytes
	Size of ReadBufferSize: 1300
	File size is (2 * 1300) - 1
If I use libstdc++ to read this file, it will read and return 2600 bytes
in two reads.
If I use low level read() for this task, it will correctly read 1300 the
first time, and 1299 the next.

This error does not happen for any other file size/ReadBufferSize
relationship that I can find! Also, the ReadBufferSize is arbitrary--if
the ReadBufferSize is 1306, and the file size is 2611, the problem will
appear. 

Here is some sample code below. I am attaching a binary file that is
2599 bytes in length as a test file.

#include <fstream>
#include <iostream>

int main(void)
{
    char *iobuf;
    static long const ReadBufferSize = 1300;

    iobuf = (char*)(operator new(ReadBufferSize));

    //test.file is attached and size is 2599 bytes
    ifstream infile("test.file",(ios::openmode)(ios::in | ios::binary));

    do 
       {
	infile.read(iobuf,ReadBufferSize);
	if(infile.gcount()
	    cout << "Amount read is " << infile.gcount() << endl;
	
        } while(infile.gcount() != 0);
}
	 

Thanks,

Jim Parsons
 <<test.file>> 

test.file


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]