如何使用C+列出连接到Linux机器的硬盘+;? 我需要用C++来列出连接到Linux机器上的硬盘驱动器。p> 有没有任何C或C++函数可用于此? < P>可以使用LybPutt/
ped\u device\u probe\u all()是检测设备的调用。否。没有标准的C或C++函数来实现这一点。你需要一个API。 但您可以使用:如何使用C+列出连接到Linux机器的硬盘+;? 我需要用C++来列出连接到Linux机器上的硬盘驱动器。p> 有没有任何C或C++函数可用于此? < P>可以使用LybPutt/,c++,linux,hard-drive,hardware-interface,C++,Linux,Hard Drive,Hardware Interface,ped\u device\u probe\u all()是检测设备的调用。否。没有标准的C或C++函数来实现这一点。你需要一个API。 但您可以使用: system("fdisk -l"); 它不是一个函数,但是您可以从/proc/partitions读取活动内核分区,或者从/sys/block的dir列表中列出所有的块设备 #include <fstream> #include <iostream> struct Mount { std::string de
system("fdisk -l");
它不是一个函数,但是您可以从/proc/partitions读取活动内核分区,或者从/sys/block的dir列表中列出所有的块设备
#include <fstream>
#include <iostream>
struct Mount {
std::string device;
std::string destination;
std::string fstype;
std::string options;
int dump;
int pass;
};
std::ostream& operator<<(std::ostream& stream, const Mount& mount) {
return stream << mount.fstype <<" device \""<<mount.device<<"\", mounted on \""<<mount.destination<<"\". Options: "<<mount.options<<". Dump:"<<mount.dump<<" Pass:"<<mount.pass;
}
int main() {
std::ifstream mountInfo("/proc/mounts");
while( !mountInfo.eof() ) {
Mount each;
mountInfo >> each.device >> each.destination >> each.fstype >> each.options >> each.dump >> each.pass;
if( each.device != "" )
std::cout << each << std::endl;
}
return 0;
}
#包括
#包括
结构安装{
字符串设备;
std::字符串目的地;
std::字符串类型;
std::字符串选项;
int转储;
国际通行证;
};
std::ostream&operator那么fdisk是如何做到的呢?你认为fdisk是用什么语言写的?@static\u rtti在fdisk上运行strace。它看起来像是打开/proc/partitions以获取设备列表,然后打开每个设备文件RO并尝试从每个设备读取分区表。。。fwiwnop,不解析fdisk输出或任何命令。它可能被翻译成用户的语言环境,或者有一个不稳定的输出。是的。我已经检查过了,但是我找不到任何资源。只是为了消除歧义,你想列出连接或装载的硬盘吗?Linux与使用文件系统的系统有一个非常好的接口。请看一下dir“/dev/disk”。列出连接或装入的硬盘就足够了。我会尝试一下并告诉您。谢谢你的建议不用担心,伙计。如果它符合您的要求,请记住将其标记为答案:)