我正在寻找一些功能,它将返回安装到/ dev / sdb的micro SD卡的总容量。我不关心自由空间我关心驱动器的总容量。我需要一个可靠而准确的功能。如果没有,我将如何创建一个?
谢谢!
答案 0 :(得分:4)
blockdev
的strace告诉我你可以使用:
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
int main()
{
unsigned long long size;
int fd = open("/dev/sdx", O_RDONLY);
ioctl(fd, BLKGETSIZE64, &size);
std::cout << size << std::endl;
std::cout << (size>>20) << std::endl; // MiBytes
}
(用设备节点名替换sdx)
注意更喜欢使用uint64_t
,如果您的编译器已经支持它(包括<cstdint>
)
答案 1 :(得分:1)
您只需阅读/sys/
目录中的特殊文件:
/sys/block/sdb/sdb1/size
以字节为单位返回大小。