当前位置: 首页 > 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/193724.html

相关文章:

  • 免费pc 微网站模板网店运营公司
  • 域名注册之后怎么进行网站建设seo是什么技术
  • 做cpa广告网站教程想要推广网页
  • 淘客网站如果做优化福州百度推广排名优化
  • 上海公安门户网站全网seo是什么意思
  • 在设计赚钱的网站有哪些黑科技引流推广神器
  • c 微网站开发百度咨询
  • 大庆网站建设公司ciliba磁力搜索引擎
  • 专业网站建设费用怎么算百度大搜
  • 网站建设咨询哪家性价比高sem推广竞价
  • 广州3d网站开发杭州seo公司服务
  • 做微信头图的网站太原网站制作优化seo
  • 汉阳区建设局网站深圳seo网站优化公司
  • 网站做中英文英文太长怎么办如何做宣传推广营销
  • 芜湖企业做网站网站优化推广价格
  • 中国建筑人事部大全seo排名优化方法
  • 如何让本机做网站让内网访问百度站长平台工具
  • 网站推广怎么做google搜索引擎入口
  • wordpress书本目录模板seo内容优化心得
  • 长沙有哪些楼盘网站优化排名方法
  • 免费自建网站杭州最专业的seo公司
  • 做视频网站软件有哪些奶茶推广软文200字
  • 做响应式的网站汕头seo网站推广
  • 西域数码网站建设搜索引擎营销的主要方式有
  • 美食门户网站建设目标seo咨询服务价格
  • 广西壮族自治区住房和建设厅网站台州seo快速排名
  • 丹阳如何做百度的网站合肥网站推广电话
  • 家政网站建设网页快速收录
  • 建设工程合同范本 政府网站西安做网站的网络公司
  • 网站 做购物车什么是推广