在Linux上,您可以使用
sysinfo在以下结构中设置值的函数:
#include <sys/sysinfo.h> int sysinfo(struct sysinfo *info); struct sysinfo { long uptime; unsigned long loads[3]; unsigned long totalram; unsigned long freeram; unsigned long sharedram; unsigned long bufferram; unsigned long totalswap; unsigned long freeswap; unsigned short procs; unsigned long totalhigh; unsigned long freehigh; unsigned int mem_unit; char _f[20-2*sizeof(long)-sizeof(int)]; };
如果您只想使用C 函数来做到这一点(我会坚持使用sysinfo
),建议您使用std::ifstream
和采取C
方法
std::string:
unsigned long get_mem_total() { std::string token; std::ifstream file("/proc/meminfo"); while(file >> token) { if(token == "MemTotal:") { unsigned long mem; if(file >> mem) { return mem; } else { return 0; } } // ignore rest of the line file.ignore(std::numeric_limits<std::streamsize>::max(), 'n'); } return 0; // nothing found}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)