简体   繁体   English

如何在C中找到已安装的USB闪存盘的大小?

[英]How do I find the size of mounted USB flash drive in C?

I have a flash drive device (/dev/sda1) mounted to /mnt on an embedded linux system (kernel 2.6.23). 我在嵌入式Linux系统(内核2.6.23)上安装了/ mnt的闪存驱动器设备(/ dev / sda1)。 Using C how do I work out the size of the drive? 使用C我如何计算出驱动器的大小?

On Linux, if you're not worried about portability (C doesn't know about drives, so any such specific code will be unportable), use statfs() : 在Linux上,如果你不担心可移植性(C不知道驱动器,那么任何这样的特定代码将是不可移植的),使用statfs()

  struct statfs fsb;

  if(statfs("/mnt", &fsb) == 0)
    printf("device has %ld blocks, each %ld bytes\n", fsb.f_blocks, fsb.f_bsize);

Read and parse a number in device's sysfs entry. 读取并解析设备的sysfs条目中的数字。 In your case, 在你的情况下,

  1. Full device (all partitions and partition table): /sys/block/sda/size 完整设备(所有分区和分区表): /sys/block/sda/size
  2. Logical partition on this device: /sys/block/sda/sda1/size 此设备上的逻辑分区: /sys/block/sda/sda1/size

The device does not have to be mounted yet. 该设备不必安装。

If you have no problem using external tools, exec this : 如果您使用外部工具没有问题,请执行以下操作:

df -h | grep -i /dev/sda1

using popen , and parse the resulting line with strtok . 使用popen ,并使用strtok解析生成的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM