views:

226

answers:

3

How can I get the file size of a file in C when the file size is greater than 4gb?

ftell returns a 4 byte signed long, limiting it to two bytes. stat has a variable of type off_t which is also 4 bytes (not sure of sign), so at most it can tell me the size of a 4gb file.

What if the file is larger than 4 gb?

+2  A: 

On Linux with glibc, ftell returns an off_t; depending on the flags off_t may be 32 bit or may be 64 bit.

On Linux, you can get the appropriate flags to have a 64 bit off_t by doing getconf LFS_CFLAGS (LFS stands for large-file-support).

R Samuel Klatchko
+1  A: 

On Windows, GetFileSize[Ex] is what you use.

Alex
+1  A: 

try

#define _LARGEFILE64_SOURCE 1
#define _FILE_OFFSET_BITS 64

i think that increases the size of off_t to 64 bits on some operating systems

Jay