如何使用 C++ 列出连接到 Linux 计算机的硬盘?

发布于 2024-12-02 06:08:10 字数 72 浏览 4 评论 0原文

我需要使用 C++ 列出连接到 Linux 计算机的硬盘驱动器。

是否有任何 C 或 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

平定天下 2024-12-09 06:08:11

没有。没有标准 C 或 C++ 函数可以做到这一点。您将需要一个 API。
但你可以使用:

system("fdisk -l");

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

system("fdisk -l");
停滞 2024-12-09 06:08:10

看看我制作的这个简单的 /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;
}

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;
}
盗梦空间 2024-12-09 06:08:10

您可以使用 libparted

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

ped_device_probe_all() 是检测设备的调用。

You can use libparted

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

ped_device_probe_all() is the call to detect the devices.

遮云壑 2024-12-09 06:08:10

它不是一个函数,但您可以从 /proc/partitions 读取活动内核分区或从 /sys/block 的目录列表中列出所有块设备

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文