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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With