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.
File Size. A file size is frequently expressed in bytes, kilobytes (kb) or megabytes (mb). A byte generally represents a single character, digit , or symbol (including a space) of data. Each byte is composed of 8 bits.
1 byte. -128 to 127. int. 2 or 4 bytes. -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With