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

How to get the file size of a file in C when the file size exceeds 4 GB?

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

What if the file is larger than 4 GB?

+3
source share
3 answers

On Linux with glibc, ftell returns off_t; depending on the flags, there off_tmay be 32 bits or there may be 64 bits.

On Linux, you can get the appropriate flags for 64-bit off_tby doing getconf LFS_CFLAGS(LFS means large file support).

+2

Windows GetFileSize[Ex] , .

+2

to 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

+1
source

Source: https://habr.com/ru/post/1744728/


All Articles