How to list hard drives connected to a Linux machine using C ++?

I need to specify hard disk drives attached to a Linux machine using C ++.

Is there a C or C ++ function available?

+4
source share
4 answers

You can use libparted

http://www.gnu.org/software/parted/api/

ped_device_probe_all () is a call to discover devices.

+6
source

This is not a function, but you can read the active kernel partitions from / proc / partition or list all blocking devices from the list of / sys / block directories

+5
source

Take a look at this simple / proc / mounts handler 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; } 
+5
source

Nope. There is no standard C or C ++ function for this. You will need an API. But you can use:

 system("fdisk -l"); 
+1
source

Source: https://habr.com/ru/post/1369661/


All Articles