C: What's the right data type to use for file sizes in bytes?
If I use something like double
to hold file sizes in bytes开发者_开发百科, this will naturally fail very soon, so which data type would I use? Nowadays a couple of TB are common, so I'd like the data type to be able to hold a number that big.
It will depends on you platform. On Unix System, you can probably use the off_t
type. On Windows, you'll probably want to use LARGE_INTEGER
. Those are the types used on those system by the function returning the file size (stat
on Unix, GetFileSize
on Windows).
If you want a portable type, uint64_t
should be large enough.
There is a function called ftello()
which returns a off_t
.
With fgetpos()
, you need a fpos_t
variable - EDIT: which is not necessarily an arithmetic type.
So off_t
could/should be appropriate to fit your needs, and keeps you independent of the actual data type sizes.
long long
is a Pop Favorite :)
You might want to look at using off_t
, since this is what standard functions such as fseeko and ftello use.
size_t is the usual for filesizes in C, but the type of it is platform dependent (32-bit or 64-bit unsigned int on Windows). I'd suggest __int64 (a 64-bit int) - should cope with up to 138,547,332 terrabytes.
EDIT: Just checking, the Windows API call GetFileSizeEx
gets you a LARGE_INTEGER, which is a 64-bit signed integer. So LARGE_INTEGER would be a good one too.
The libc stat
uses off_t
, which is a typedef for an unsigned, 64-bit wide type. That would be suitable for a little while.
精彩评论