Reputation: 12054
I have written a code to calculate the hardisk size, but for some reason it always gives the size lesser than the actual size.
Like, 80GB will show as 74GB, and 160GB will show as 149 GB. Where is the catch?
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <linux/fs.h>
int main()
{
long bytes = 0;
int fd = open("/dev/sdb1", O_RDONLY);
const unsigned long long a = (1024ULL* 1024ULL * 1024ULL);
int retval = ioctl(fd, BLKGETSIZE64, &bytes);
int hdSize = bytes / a;
printf(" Harddisk = %lld \n",hdSize);
return EXIT_SUCCESS;
}
Upvotes: 1
Views: 306
Reputation: 1
As well as the difference between gigabytes and gibibytes, there's also the formatting of the partition(s) to be aware of.
All the metadata and directory structures (and journal files...) will steal some of the available space.
Another reason may be hidden rescue/restoration partitions, which pinch 5-10 GB from the end of a hard drive.
Upvotes: 0
Reputation: 399703
Simply different Gigabyte definitions. You use 1 GB = 230 bytes, many harddrive vendors use 109 bytes.
For instance, an "80 GB" disk from a manufacturer using the latter definition will hold 80,000,000,000 bytes, which equals 78,125,000 KB, 76,294 MB, or (as you verified) 74.51 GB where all my units use power-of-two definitions.
Upvotes: 8