4

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?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
endeavormac
  • 659
  • 2
  • 8
  • 18

3 Answers3

2

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
  • 74,869
  • 16
  • 134
  • 187
  • glibc documentation shows `ftell` returning `long int` and `ftello` returning `off_t`. http://www.gnu.org/software/libc/manual/html_node/File-Positioning.html – Gavin Smith Mar 20 '14 at 02:26
2

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

Alex Budovski
  • 17,947
  • 6
  • 53
  • 58
1

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
  • 9,314
  • 7
  • 33
  • 40