5

C++ を使用して、Linux マシンに接続されているハードディスク ドライブを一覧表示する必要があります。

これを行うために利用できる C または C++ 関数はありますか?

4

4 に答える 4

8

私が作成したこの単純な /proc/mounts パーサーを見てください。

#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;
}
于 2011-08-30T14:37:16.423 に答える
6

libpartedを使用できます

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

ped_device_probe_all()は、デバイスを検出するための呼び出しです。

于 2011-08-30T13:38:25.150 に答える
5

これは関数ではありませんが、/proc/partitions からアクティブなカーネル パーティションを読み取るか、/sys/block のディレクトリ リストからすべてのブロック デバイスをリストできます。

于 2011-08-30T13:43:07.410 に答える
0

いいえ。それを行うための標準のC または C++ 関数はありません。API が必要になります。ただし、次を使用できます。

system("fdisk -l");
于 2011-08-30T13:39:28.520 に答える