views:

432

answers:

2

I have a huge binary file which is 2148181087 bytes (> 2gb)

I am trying to do fopen (file, "r") and it failed with

Can not open: xyz file (Value too large to be stored in data type)

I read on the man page EOVERFLOW error is received when the file size > 2gb.

The weird thing is, I use a different input file which is also "almost" as big as the first file 2142884400 bytes (also >2gb), fopen works fine with this.

Is there any cutoff on the file size for fopen or is there any alternate way to solve this?

+6  A: 

The cutoff is 2GB which, contrary to what you may think, is not 2,000,000,000 (2x10003).

It's 2,147,483,648 (2x10243). So your second file, which works, is actually less than 2GB in size).

2GB, in the computer world, is only 2,000,000,000 in the minds of hard drive manufacturers so they can say their disks are bigger than they really are :-) - it lets them say their disks are actually 2.1GB.

paxdiablo
got it..thanks for clarifying the size calculation :-)
Jitesh Dani
I don't believe a computer maker would market 2G=2*10^9
jpinto3912
@jpinto3912: http://en.wikipedia.org/wiki/Gigabyte : "Today the usage of the unit gigabyte is still ambiguous: its value may depend on the context of usage. When referring to disk storage capacities it usually means 1000^3 bytes."
paxdiablo
+3  A: 

The "alternative way to solve this" depends on which operating system/library you are using.

For the GNU C library, you can use fopen64 as a replacement for fopen; it uses 64-bit file handles (there's also a macro to have fopen use 64-bit file handles).

For Windows, you'll probably have to switch to the Win32 file management API, with which you can use CreateFile.

James McNellis
I just found out from the man page about fopen64 bit system call. I am currently on the UNIX platform. I just tried this system call and it worked. What do you mean by 64 bit file handles? ) Is it to do something with 64 bit boundaries? Will it have any adverse effect while reading the binary file in the application if I use fopen64()?
Jitesh Dani
The `FILE*` it returns is still the same type as returned by `fopen`, it just has the `O_LARGEFILE` flag set. The file handle can be used like any other file handle with the typical I/O functions.
James McNellis
Thanks.............
Jitesh Dani