Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tellg() returning negative answer

Tags:

c++

file-io

I have opened a file in binary mode, and doing below operations gives negative value of x. The file that I have opened is ~2.5 GB in size.

infile.seekg(0, ios::end);
__int64 x = infile.tellg();

I needed infile to read bytes (unsigned chars), so I had defined it as a uifstream by doing:

typedef basic_ifstream<unsigned char, std::char_traits<unsigned char> > uifstream;

which is basically a standard ifstream, but with unsigned chars instead of chars.

EDIT: I am using Visual Studio 2005 and corrected uofstream to uifstream.

like image 300
c0da Avatar asked Nov 04 '11 09:11

c0da


1 Answers

I already put this in a comment but I think it is also the answer:

STL in VS2005 does not support offsets larger than 2147483647 (~2GB) so seeking or telling the position beyond that does not work and would explain the negative values. (see here)

(Also tellg() returns -1 when there is an error, but I assume you are seeing other negative values)

The solution is to use a newer compiler (VS2010) or use an alternative STL implementation like STLPort

like image 143
rve Avatar answered Oct 12 '22 18:10

rve