3

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?

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222

1 Answers1

13

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Downvoter, care to explain? I'd like to know if I've made a mistake. – Fred Foo Sep 26 '14 at 17:39
  • On 32-bit Linux Ubuntu 13.x here. Having this issue with fopen, without fseek. `#define _FILE_OFFSET_BITS 64` is needed to get the program to even open large files. So, no, it is not always OS limitations but sometimes just hard-coded garbage in C libs. – Andrew Johnson Sep 26 '14 at 21:26
  • @AndrewJohnson That doesn't contradict my answer: you need at least OS support for this to work, and `_FILE_OFFSET_BITS` is in the answer. – Fred Foo Sep 27 '14 at 08:18
  • 1
    It directly contradicts your first paragraph. My OS supports such files, but I am not able to just `fopen` them. The preprocessor directive is needed for C to compile with large file support. – Andrew Johnson Sep 27 '14 at 09:53
  • @AndrewJohnson: ok. Made the answer more precise, please check. – Fred Foo Sep 27 '14 at 10:08