gpt4 book ai didi

计算硬盘大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:18:36 26 4
gpt4 key购买 nike

我写了一段代码来计算硬盘大小,但由于某种原因,它给出的大小总是小于实际大小。

例如,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;
}

最佳答案

简单different Gigabyte definitions .您使用 1 GB = 230 字节,许多硬盘供应商使用 109 字节。

例如,来自使用后一种定义的制造商的“80 GB”磁盘将包含 80,000,000,000 字节,等于 78,125,000 KB、76,294 MB 或(如您验证的那样)74.51 GB,其中我的所有单元都使用二次幂定义。

关于计算硬盘大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11578747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com