Jeegar Patel
Jeegar Patel

Reputation: 27240

how to handle file whose size is more than 2 GB?

in c fseek,fopen this all function works for long int means only handle file of 2 gb. now how can i open file whose size is more than 2 GB?

Upvotes: 3

Views: 3285

Answers (1)

Fred Foo
Fred Foo

Reputation: 363858

As the commenters have already explained, whether you can open a file of more than 2GB depends on the OS and C library, not on the compiler on sizeof(long). If your OS supports such files, you should be able to fopen them, although you may have to set a flag (#define _FILE_OFFSET_BITS 64 for Linux).

Then, fseek indeed cannot seek to positions farther away than LONG_MAX in a single call. You can either call fseek several times in a loop, which can be cumbersome, or check if your platform has fseeko which takes an offset argument of type off_t. That type should be big enough to capture the size of any (regular) file on your system if you set the right options. fseeko is available on newer Linux and all POSIX-2001-compliant OSs.

Upvotes: 13

Related Questions