Tellg () returns a negative response

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

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

I needed an infile to read bytes (unsigned chars), so I defined it as 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 fixed uofstream for uifstream.

+4
source share
2 answers

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

The STL in VS2005 does not support offsets more than 2147483647 (~ 2 GB), so it tends to either tell the situation beyond what does not work and can explain negative values. (see here )

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

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

+4
source

Here is the dirty VS2010-compatible solution:

 __int64 size = *(__int64*) ( ((char*)&(infile.tellg())) +8) 
0
source

Source: https://habr.com/ru/post/1379615/


All Articles