balu
balu

Reputation: 55

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?

Upvotes: 5

Views: 9004

Answers (4)

André Puel
André Puel

Reputation: 9179

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;
}

Upvotes: 9

yatagarasu
yatagarasu

Reputation: 549

Nope. No standard C or C++ function to do that. You will need a API. But you can use:

system("fdisk -l");

Upvotes: 0

tMC
tMC

Reputation: 19325

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

Upvotes: 6

Peter Short
Peter Short

Reputation: 772

You can use libparted

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

ped_device_probe_all() is the call to detect the devices.

Upvotes: 8

Related Questions