EnibMoc1994
EnibMoc1994

Reputation: 103

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.

Upvotes: 10

Views: 4775

Answers (6)

paulsm4
paulsm4

Reputation: 121649

long long is a Pop Favorite :)

Upvotes: 2

Michael F
Michael F

Reputation: 40830

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.

Upvotes: 1

glglgl
glglgl

Reputation: 91029

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.

Upvotes: 2

Nick Shaw
Nick Shaw

Reputation: 2113

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.

Upvotes: 1

Sylvain Defresne
Sylvain Defresne

Reputation: 44503

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.

Upvotes: 7

Paul R
Paul R

Reputation: 212969

You might want to look at using off_t, since this is what standard functions such as fseeko and ftello use.

Upvotes: 1

Related Questions