Skip to content

Instantly share code, notes, and snippets.

@NanXiao
Created June 4, 2019 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NanXiao/88c733ca4454da7b459ae91adcbc1cb6 to your computer and use it in GitHub Desktop.
Save NanXiao/88c733ca4454da7b459ae91adcbc1cb6 to your computer and use it in GitHub Desktop.
Parse /proc/meminfo file
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
int main()
{
const std::string meminfo_file("/proc/meminfo");
std::ifstream ifs(meminfo_file);
if (ifs)
{
std::string line;
while (std::getline(ifs, line))
{
std::istringstream iss(line);
std::string name;
std::string value;
if (iss >> name >> value)
{
name.pop_back();
std::cerr << name << ' ' << value << '\n';
}
else
{
std::cerr << meminfo_file << "has wrong format: " << line << '\n';
return 1;
}
}
return 0;
}
else
{
std::cerr << "No " << meminfo_file << '\n';
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment