当前位置: 首页 > wzjs >正文

网站开发广告宣传语网站测试报告

网站开发广告宣传语,网站测试报告,网站改版,建立企业网站的形式有现代c获取linux系统磁盘大小 前言一、命令获取系统磁盘大小二、使用c获取系统磁盘大小三、总结 前言 本文介绍一种使用c获取linux系统磁盘大小的方法 一、命令获取系统磁盘大小 在linux系统中可以使用lsblk命令显示当前系统磁盘大小,如下图所示 lsblk二、使用c获…

现代c++获取linux系统磁盘大小

  • 前言
  • 一、命令获取系统磁盘大小
  • 二、使用c++获取系统磁盘大小
  • 三、总结

前言

本文介绍一种使用c++获取linux系统磁盘大小的方法

一、命令获取系统磁盘大小

linux系统中可以使用lsblk命令显示当前系统磁盘大小,如下图所示

lsblk

在这里插入图片描述

二、使用c++获取系统磁盘大小

  • 在使用c++获取磁盘大小之前我们先了解一下linux下的/sys/block目录,有兴趣的可以去详细了解下,我这里就不卖关子了,首先呢/sys/block目录存放的是所有的块设备文件,也就是系统磁盘分区。如下图所示
    在这里插入图片描述

  • 上图中的每一个子目录如loop0mmcblk1zram0等都是系统上的磁盘分区文件,也就是说每一个子目录都是一个磁盘分区设备,可以理解为当前系统有子目录这么多的块设备,如果说我们把所有块设备的大小加起来不就是我们磁盘大小了吗,没错,我们就是要这样干。

  • 首先,每个块设备都是一个目录,这个目录下存放的有该块设备的一些信息,如下图所示
    在这里插入图片描述

  • 我们重点关注的是这个size文件,这个文件中存放的是这个块设备所占有的磁盘扇区数(注意不是字节数,是扇区数量)如下图所示
    在这里插入图片描述

  • 接下来我们要知道一个扇区多少个字节呢,这个是存放在每个块设备目录下的queue/hw_sector_size里的
    在这里插入图片描述
    在这里插入图片描述

  • 接下来我们要知道这些块设备哪些是虚拟的块设备,哪些是实际的块设备,有一个典型的区分方法,就是如果是虚拟的块设备,它的子目录下通常没有device目录,而实际设备是有device目录的
    在这里插入图片描述
    在这里插入图片描述

  • 接下来我们整理一下思路

    1. 收集/sys/block下的所有子目录名称(块设备)
    2. 根据块设备目录下是否有device目录来判断它是否是虚拟设备,排除所有的虚拟设备,得到实际块设备
    3. 读取实际块设备目录下的size文件获取该块设备占用的扇区数。
    4. 读取实际块设备目录下的queue/hw_sector_size获取一个扇区占用多少字节。
    5. 得到一个(实际块设备占用的字节数) = (该块设备占用扇区数) * (该块设备扇区占用字节数)。
    6. 遍历所有的实际块设备得到他们占用的字节数,再相加,就是系统磁盘大小。
  • 先写一些辅助方法,如判断文件或者文件夹是否存在的方法, 如果文件中只有一个数字,获取该数字的方法

#include <string>
#include <filesystem> // 需要c++17支持
#include <fstream>
bool directIsExists(const std::string &path) {return std::filesystem::exists(path);
}std::size_t getFileNumber(const std::string &path) {std::size_t number = 0;std::ifstream ifs(path);if (!ifs.is_open()) {return number;}ifs >> number;return number;
}

如果不支持c++17,可以使用下面方法平替

#include <string>
#include <unistd.h>
#include <fstream>
bool directIsExists(const std::string &path) {return access(path.c_str(), F_OK) == 0;
}std::size_t getFileNumber(const std::string &path) {std::size_t number = 0;std::ifstream ifs(path);if (!ifs.is_open()) {return number;}ifs >> number;return number;
}
  • 收集/sys/block目录下的所有实际块设备
#include <string>
#include <iostream>
#include <filesystem>
#include <vector>
std::vector<std::string> getBlockDevices(void) {std::vector<std::string> subdirs;try {for (const auto &entry : std::filesystem::directory_iterator("/sys/block")) {if (std::filesystem::is_directory(entry.status())) {if (directIsExists("/sys/block/" + entry.path().filename().string() + "/device")) {subdirs.push_back(entry.path().filename().string());}}}}catch (const std::filesystem::filesystem_error &e) {std::cerr << "Error accessing : " << e.what() << std::endl;}return subdirs;
}

如果不支持c++17,可以使用下面方法平替

#include <vector>
#include <string>
#include <iostream>
#include <dirent.h>
std::vector<std::string> getBlockDevices() {std::vector<std::string> subdirs;DIR *dir = opendir("/sys/block");if (dir == nullptr) {perror("opendir");return subdirs;}struct dirent *entry;while ((entry = readdir(dir)) != nullptr) {if (std::string(entry->d_name) != "." && std::string(entry->d_name) != "..") {if (directIsExists("/sys/block/" + std::string(entry->d_name) + "/device")) {subdirs.push_back(entry->d_name);}}}closedir(dir);return subdirs;
}
  • 获取指定块设备的扇区数
std::size_t getSectorNumber(const std::string &path) {return getFileNumber(path);
}
  • 获取指定块设备的扇区的大小
std::size_t getSectorSize(const std::string &path) {return getFileNumber(path);
}
  • 获取总大小
std::size_t getDiskSize(void) {std::size_t number = 0;for (const auto &it : getBlockDevices()) {std::cout << it << std::endl;std::cout << getSectorNumber("/sys/block/" + it + "/size") << std::endl;std::cout << getSectorSize("/sys/block/" + it + "/queue/hw_sector_size") << std::endl;number += getSectorNumber("/sys/block/" + it + "/size") * getSectorSize("/sys/block/" + it + "/queue/hw_sector_size");}return number;
}

完整代码如下

#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <unistd.h>
#include <vector>
bool directIsExists(const std::string &path) {return std::filesystem::exists(path);
}std::vector<std::string> getBlockDevices(void) {std::vector<std::string> subdirs;try {for (const auto &entry : std::filesystem::directory_iterator("/sys/block")) {if (std::filesystem::is_directory(entry.status())) {if (directIsExists("/sys/block/" + entry.path().filename().string() + "/device")) {subdirs.push_back(entry.path().filename().string());}}}}catch (const std::filesystem::filesystem_error &e) {std::cerr << "Error accessing : " << e.what() << std::endl;}return subdirs;
}// #include <dirent.h>// bool directIsExists(const std::string &path) {
//     return access(path.c_str(), F_OK) == 0;
// }
// std::vector<std::string> getBlockDevices() {
//     std::vector<std::string> subdirs;//     DIR *dir = opendir("/sys/block");
//     if (dir == nullptr) {
//         perror("opendir");
//         return subdirs;
//     }//     struct dirent *entry;
//     while ((entry = readdir(dir)) != nullptr) {
//         if (std::string(entry->d_name) != "." && std::string(entry->d_name) != "..") {
//             if (directIsExists("/sys/block/" + std::string(entry->d_name) + "/device")) {
//                 subdirs.push_back(entry->d_name);
//             }
//         }
//     }//     closedir(dir);
//     return subdirs;
// }std::size_t getFileNumber(const std::string &path) {std::size_t number = 0;std::ifstream ifs(path);if (!ifs.is_open()) {return number;}ifs >> number;return number;
}std::size_t getSectorNumber(const std::string &path) {return getFileNumber(path);
}std::size_t getSectorSize(const std::string &path) {return getFileNumber(path);
}std::size_t getDiskSize(void) {std::size_t number = 0;for (const auto &it : getBlockDevices()) {number += getSectorNumber("/sys/block/" + it + "/size") * getSectorSize("/sys/block/" + it + "/queue/hw_sector_size");}return number;
}int main(int argc, char **argv) {auto num = getDiskSize();std::cout << "bytes --> " << num << std::endl;std::cout << num / 1024.0 << "kb" << std::endl;std::cout << num / 1024.0 / 1024.0 << "Mb" << std::endl;std::cout << num / 1024.0 / 1024.0 / 1024.0 << "Gb" << std::endl;return 0;
}

编译并执行, g++ main.cpp -o main -std=c++17 && ./main, 执行结果如下
在这里插入图片描述
在这里插入图片描述

三、总结

通过上诉方法确实可以获取磁盘容量,亲测可用!!!

http://www.dtcms.com/wzjs/445872.html

相关文章:

  • 做网站都能赚钱吗全网营销推广怎么做
  • 重庆低价网站建设站长工具seo优化建议
  • 免费域名网站申请大数据营销系统软件
  • 企业网站建设排名客服正规seo排名公司
  • 广西棋牌软件开发公司seo推广培训学费
  • 温州龙湾网站建设网站出租三级域名费用
  • 企业网站模板价格2020 惠州seo服务
  • 电影网站内页360推广和百度推广哪个好
  • 做网站服务器是必须购买的吗人员优化是什么意思
  • 刷网站排名软件线上商城推广软文
  • 龙岗网站建设公司百度推广优化怎么做的
  • 物联网技术应用专业是学什么的学seo网络推广
  • 自己怎么做农好产品网站seo自动工具
  • 怎么查看网站disallowaso推广优化
  • 连云港网站建设方案国际时事新闻
  • 有口碑的徐州网站建设搜索引擎技术
  • 南宁疫情封路最新消息官网优化哪家专业
  • 网站做301重定向怎么做2024年1月新冠高峰期
  • 一家做土产网站西点培训学校
  • 如何优化移动端网站口碑营销什么意思
  • 免费网站建设阿里云合肥seo培训
  • 最讨厌网站网站结构优化的内容和方法
  • 宁波网站建设推广公司重庆百度快照优化排名
  • 网站桥页怎么找郑州网站建设制作公司
  • 做网站要费用多少网站seo基本流程
  • 网站开发 动易友情链接平台广告
  • 山东电力建设河北分公司网站在线培训
  • 做照片书的网站好谷歌浏览器搜索引擎入口
  • 自己做网站转发新闻违法么网络运营推广是做什么的
  • 单位做网站的目的设计个人网站