How to list the harddisks attached to a Linux machine using C++?
I need to list the harddisk drives attached to the Linux开发者_运维知识库 machine using the C++.
Is there any C or C++ function available to do this?
Take a look at this simple /proc/mounts parser I made.
#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;
}
You can use libparted
http://www.gnu.org/software/parted/api/
ped_device_probe_all() is the call to detect the devices.
Its not a function, but you can read the active kernel partitions from /proc/partitions or list all the block devices from dir listing of /sys/block
Nope. No standard C or C++ function to do that. You will need a API. But you can use:
system("fdisk -l");
精彩评论