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?
Asked
Active
Viewed 2,926 times
3
-
3This is a platform specific issue, not a "C" issue. – Arafangion Aug 08 '11 at 09:26
-
you mean if i have 64 bit platform than i can ? – Jeegar Patel Aug 08 '11 at 09:27
-
http://stackoverflow.com/questions/2091304/handling-files-greater-than-2-gb-in-msvc6 or http://www.gnu.org/software/libc/manual/html_node/Opening-Streams.html#index-fopen64-931 – cristian Aug 08 '11 at 09:29
-
2@Mr.32: That depends. How does that 64-bit platform implement fseek? – Arafangion Aug 08 '11 at 09:32
-
3Change your nickname to Mr.64. – user703016 Aug 08 '11 at 09:37
-
1fopen doesn't depend on the size of long. If you don't need to fseek, but just read the file sequentially you should be fine. Also, on **some** 64-bit systems `long` is 64 bit. – Bo Persson Aug 08 '11 at 09:47
-
What platform are you in ? On linux there are 64 bit versions of IO functions: `fopen64`, `fseek64`, etc – Alexandre C. Aug 08 '11 at 10:04
-
@Alexandre C. ya i m working on 32 bit linux – Jeegar Patel Aug 08 '11 at 10:08
-
2See http://stackoverflow.com/questions/1035657/seeking-and-reading-large-files-in-a-linux-c-application . Closing as duplicate – Alexandre C. Aug 08 '11 at 10:09
1 Answers
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
-
-
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
-
1It 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
-