Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: What's the right data type to use for file sizes in bytes?

Tags:

c

types

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.

like image 953
EnibMoc1994 Avatar asked Sep 08 '11 08:09

EnibMoc1994


People also ask

What data type is file size?

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.

What is byte data type in C?

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.


2 Answers

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.

like image 179
Sylvain Defresne Avatar answered Dec 12 '22 05:12

Sylvain Defresne


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.

like image 33
glglgl Avatar answered Dec 12 '22 05:12

glglgl