Найти в Дзене
2 подписчика

Сегодня у нас будет простой код на с++ который будет считывать память в %.


#include <iostream>
#include <fstream>
#include <string>

int main() {
std::ifstream statFile("/proc/stat");
std::string line;

while (std::getline(statFile, line)) {
if (line.substr(0, 9) == "MemTotal:") {
int totalSpace = std::stoi(line.substr(10));
std::getline(statFile, line);
int freeSpace = std::stoi(line.substr(11));
int usedSpace = totalSpace - freeSpace;
float usagePercentage = (usedSpace / static_cast<float>(totalSpace)) * 100;

std::cout << "Used space: " << usagePercentage << "%\n";
break;
}
}

statFile.close();
return 0;
}
Около минуты