endeavormac
endeavormac

Reputation: 689

How do I get the file size of a large (> 4 GB) file?

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?

Upvotes: 4

Views: 2965

Answers (3)

Jay
Jay

Reputation: 9582

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

Upvotes: 1

Alex Budovski
Alex Budovski

Reputation: 18436

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

Upvotes: 2

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

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).

Upvotes: 2

Related Questions