Created
June 4, 2019 05:59
-
-
Save NanXiao/88c733ca4454da7b459ae91adcbc1cb6 to your computer and use it in GitHub Desktop.
Parse /proc/meminfo file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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