From the documentation for stat()
:
On some Unix systems (such as Linux), the following attributes may also be available:
- st_blocks - number of 512-byte blocks allocated for file
- st_blksize - filesystem blocksize
- st_rdev - type of device if an inode device
- st_flags - user defined flags for file
What you seem to want is st_blocks * st_blksize
. Do note that this is not the actual size of the file, which is the st_size
member. The number of blocks multiplies by the block size will be larger than the actual file size.
Note: When it says "st_blocks - number of 512-byte blocks allocated for file", the number 512
is actually system-dependent. The POSIX specification says
The unit for the st_blocks member of the stat structure is not defined within POSIX.1-2008. In some implementations it is 512 bytes. It may differ on a file system basis.
If the st_block
attribute is available, then don't be surprised if it's some other value.