日期:2014-05-16  浏览次数:20875 次

在linux底下如何用c语言来实现“获得所有磁盘列表”这个功能的编程,谢谢!
请高手指点,有源码更好,谢谢!

------解决方案--------------------
这不属于c的范畴,c中好象没有提供这要的调用.这属于操作系统的问题.
如果你在linux下,可用查看一个/proc/partitions
这是个内核影响文件.里边保存了系统内的磁盘信息.
------解决方案--------------------
#include <stdlib.h>

int main()
{
system( "cat /proc/partitions > > 1.txt ");
}

上面将磁盘列表写入1.txt,然后在C里打开1.txt得到
------解决方案--------------------
#define PROC_PARTITIONS "/proc/partitions "

/*
* for fdisk -l:
* try all things in /proc/partitions that look like a full disk
*/
static void
tryprocpt(void) {
FILE *procpt;
char line[100], ptname[100], devname[120];
int ma, mi, sz;

procpt = fopen(PROC_PARTITIONS, "r ");
if (procpt == NULL) {
fprintf(stderr, _( "cannot open %s\n "), PROC_PARTITIONS);
return;
}

while (fgets(line, sizeof(line), procpt)) {
if (sscanf (line, " %d %d %d %[^\n ] ",
&ma, &mi, &sz, ptname) != 4)
continue;
snprintf(devname, sizeof(devname), "/dev/%s ", ptname);
if (is_probably_full_disk(devname))
try(devname, 0);
}
fclose(procpt);
}

来源于:
util-linux-2.12r/fdisk/fdisk.c