通过C ++获取微型SD卡的大小 [英] Getting the size of a micro SD card via C++

查看:185
本文介绍了通过C ++获取微型SD卡的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些功能,将返回安装到/ dev / sdb的micro SD卡的总容量。我不关心自由空间我关心的是驱动器的总容量。我需要一个可靠准确的功能。如果没有存在我将如何创建一个?

I am looking for some function that will return the total capacity of a micro SD card mounted to /dev/sdb. I do not care so much about free space I care about total capacity of the drive. I need a reliable and accurate function. If there is none in existence how would I go about creating one?

谢谢!

推荐答案

strace for blockdev 告诉我你可以使用:

strace for blockdev tells me you could use:

#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)

(replace sdx by device node name)

注意喜欢使用 uint64_t ,如果您的编译器已经支持它(包括 cstdint>

Note prefer using uint64_t if your compiler supports it already (include <cstdint>)

这篇关于通过C ++获取微型SD卡的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆