Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Linux C编程:如何获取设备的分区信息?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我是Linux c编程的新手,有没有可以获取设备分区信息的API?最佳答案Akash Rawal的答案非常接近.一个好方法是使用libblkid.这是获取分区的uuid的示例:Using libblkid to find UUID of a partition.我将上面显示的代码和libblkid参菜单中的示例结合起来并生成以下工作程序:#include

我是Linux c编程的新手,有没有可以获取设备分区信息的API?

最佳答案
Akash Rawal的答案非常接近.一个方法是使用libblkid.

这是获取分区的uuid的示例:Using libblkid to find UUID of a partition.

我将上面显示代码和libblkid参菜单中的示例结合起来并生成以下工作程序:

#include <>dio.h>
#include Failed to open %s",argv[1]);
   }

   // Get number of partitions
   blkid_partlist ls;
   int nparts,i;

   ls = blkid_probe_get_partitions(pr);
   nparts = blkid_partlist_numof_partitions(ls);
   printf("number of partitions:%d\n",nparts);

   if (nparts <= 0){="" printf("please="" enter="" correct="" device="" name!="" e.g.="" \"/dev/sdc\"\n");="" return;="" }="" get="" uuid,label="" and="" type="" const="" char="" *uuid;="" const="" char="" *label;="" const="" char="" *type;="" for="" (i="0;" i="">< nparts;="" i++)="" {="" char="" dev_name[20];="">%s%d",argv[1],(i+1));

      pr = blkid_new_probe_from_filename(dev_Name);
      blkid_do_probe(pr);

      blkid_probe_lookup_value(pr,"UUID",&uuid,null);

      blkid_probe_lookup_value(pr,"LABEL",&label,"TYPE",&type,null);

      printf("Name=%s,UUID=%s,LABEL=%s,TYPE=%s\n",dev_name,uuid,label,typE);

   }

   blkid_free_probe(pr);

   return 0;
}

用法

gcc -o getuuid getuuid.c -lblkid
sudo ./getuuid /dev/sdc
number of partitions:1
Name=/dev/sdc1,UUID=754A-CE25,LABEL=KINGSTON,TYPE=vfat

大佬总结

以上是大佬教程为你收集整理的Linux C编程:如何获取设备的分区信息?全部内容,希望文章能够帮你解决Linux C编程:如何获取设备的分区信息?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。