我已经编写了一个计算硬盘大小的代码,但由于某种原因它总是给出的尺寸小于实际大小。计算硬盘大小
就像80GB显示为74GB,而160GB显示为149GB。 渔获在哪里?
#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;
}
是否有任何方法可以完成并取消这种差异以便每次获得实际大小? – kingsmasher1 2012-07-20 11:52:28
@ kingsmasher1我不确定你的意思。你的代码确实得到了正确的实际大小,只用gibibytes而不是千兆字节。 – Maxpm 2012-07-20 11:53:43
unsigned long long a =(1000000000ULL); – 2012-07-20 11:54:01