在C++开发过程中,获取硬盘分区的挂载点与剩余空间是常见的需求,比如开发磁盘清理工具、存储监控程序时都需要用到这类数据。C++17标准引入的filesystem库为跨平台的文件系统操作提供了统一接口,无需针对不同操作系统编写不同的系统调用代码,就能实现分区信息的获取。
核心原理说明
C++ filesystem库中的std::filesystem::space函数可以获取指定路径所在文件系统的空间信息,包括总容量、空闲容量和可用容量。而遍历所有挂载点可以通过std::filesystem::directory_iterator遍历系统的挂载点目录实现,不同系统的挂载点根目录存在差异,需要针对性处理。
跨平台适配要点
不同操作系统的挂载点规则不同:
- Windows系统的挂载点通常是各个盘符的根目录,比如C:、D:等
- Linux系统的挂载点通常在/目录下的各个挂载子目录,也可以通过遍历/proc/mounts文件获取更精准的挂载信息
- macOS系统的挂载点规则和Linux类似,根目录为/
Windows平台实现示例
Windows下可以通过遍历A到Z的盘符,判断是否为有效挂载点,再调用space函数获取空间信息,代码如下:
#include <iostream>
#include <filesystem>
#include <windows.h>
int main() {
// 遍历所有可能的盘符
for (char drive = 'A'; drive <= 'Z'; ++drive) {
std::string drive_path = std::string(1, drive) + ":\";
// 判断盘符是否存在
if (GetDriveTypeA(drive_path.c_str()) == DRIVE_FIXED || GetDriveTypeA(drive_path.c_str()) == DRIVE_REMOVABLE) {
try {
std::filesystem::path path(drive_path);
// 获取空间信息
std::filesystem::space_info info = std::filesystem::space(path);
// 转换为GB单位,1GB = 1024*1024*1024字节
double total_gb = static_cast<double>(info.capacity) / (1024 * 1024 * 1024);
double free_gb = static_cast<double>(info.free) / (1024 * 1024 * 1024);
double available_gb = static_cast<double>(info.available) / (1024 * 1024 * 1024);
std::cout << "挂载点: " << drive_path << std::endl;
std::cout << "总空间: " << total_gb << " GB" << std::endl;
std::cout << "空闲空间: " << free_gb << " GB" << std::endl;
std::cout << "可用空间: " << available_gb << " GB" << std::endl;
std::cout << "------------------------" << std::endl;
} catch (const std::filesystem::filesystem_error& e) {
// 忽略无法访问的盘符
continue;
}
}
}
return 0;
}
Linux/macOS平台实现示例
Linux和macOS下可以遍历根目录下的挂载点,或者通过读取系统的挂载信息文件获取所有挂载点,以下是遍历根目录的实现示例:
#include <iostream>
#include <filesystem>
#include <vector>
#include <fstream>
#include <sstream>
// 获取所有挂载点,Linux下可以读取/proc/mounts文件
std::vector<std::string> get_mount_points() {
std::vector<std::string> mount_points;
#ifdef __linux__
std::ifstream mount_file("/proc/mounts");
std::string line;
while (std::getline(mount_file, line)) {
std::istringstream iss(line);
std::string device, mount_point;
iss >> device >> mount_point;
mount_points.push_back(mount_point);
}
#elif defined(__APPLE__)
// macOS下可以遍历/Volumes目录获取挂载点
for (const auto& entry : std::filesystem::directory_iterator("/Volumes")) {
if (entry.is_directory()) {
mount_points.push_back(entry.path().string());
}
}
// 添加根目录
mount_points.push_back("/");
#endif
return mount_points;
}
int main() {
auto mount_points = get_mount_points();
for (const auto& mp : mount_points) {
try {
std::filesystem::path path(mp);
std::filesystem::space_info info = std::filesystem::space(path);
double total_gb = static_cast<double>(info.capacity) / (1024 * 1024 * 1024);
double free_gb = static_cast<double>(info.free) / (1024 * 1024 * 1024);
double available_gb = static_cast<double>(info.available) / (1024 * 1024 * 1024);
std::cout << "挂载点: " << mp << std::endl;
std::cout << "总空间: " << total_gb << " GB" << std::endl;
std::cout << "空闲空间: " << free_gb << " GB" << std::endl;
std::cout << "可用空间: " << available_gb << " GB" << std::endl;
std::cout << "------------------------" << std::endl;
} catch (const std::filesystem::filesystem_error& e) {
// 忽略无法访问的挂载点
continue;
}
}
return 0;
}
注意事项
使用上述代码时需要注意以下几点:
- 编译时需要开启C++17及以上标准,比如使用g++编译时添加-std=c++17参数
- 获取空间信息时,capacity表示文件系统总容量,free表示空闲的总字节数,available表示普通用户可用的字节数,两者可能存在差异
- 部分特殊分区可能无法访问,需要做好异常捕获,避免程序崩溃
- 如果只需要获取指定路径所在分区的空间,直接调用
std::filesystem::space(指定路径)即可,无需遍历所有挂载点
C++filesystem硬盘分区挂载点剩余空间修改时间:2026-06-11 20:43:02