下面通过一个C++代码读取arm设备的内核和内存信息,把他写在文件中
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string>
#include<cstring>
using namespace std;
void getNumStr(int a, char* nums){
memset(nums, '\0', 20);
int size = 0, i;
while(a > 0){
int ige = a % 10;
a /= 10;
nums[size++] = ige + '0';
}
if(size){
for(i=0;i<size/2;i++){
char temp = nums[i];
nums[i] = nums[size-i-1];
nums[size-i-1] = temp;
}
}
}
int main(int argc, char* argv[]){
// First, U need save system's information
// U can call the order "uname -a > info.txt" directly
int reHead = system("uname -a > info.txt");
for(int i=0;i<1;i++){
if(!reHead){
// Next, U need to get CPUs' infomation
// To get how many cpus it has
reHead = system("cat /proc/cpuinfo | grep processor | wc -l > temp.txt");
int howManyCpus = 0;
if(!reHead){
ifstream iftemp("temp.txt");
iftemp >> howManyCpus;
iftemp.close();
// Append to info.txt
if(howManyCpus > 1){
char nums[20];
char echos[30];
getNumStr(howManyCpus, nums);
strcpy(echos, "echo There are ");
strcat(echos, nums);
strcat(echos, " CPU: >> info.txt");
system(echos);
system("echo -------------------------------------------------------------
------ >> info.txt");
system("cat /proc/cpuinfo >> info.txt");
}else if(howManyCpus > 0){
system("echo Get CPU info error >> info.txt");
}
}
// To get memery's infomation
system("free > temp.txt");
ifstream ifs("temp.txt");
char temp[20];
int total, used, free, shared, buffers, cached;
ifs >> temp >> temp >> temp >> temp >> temp >> temp >> temp;
ifs >> total;
ifs >> used;
ifs >> free;
ifs >> shared;
ifs >> buffers;
ifs >> cached;
ifs.close();
system("echo Memery Infomation: >> info.txt");
system("echo -----------------------------------------------------------------
-- >> info.txt");
ofstream ofs_("info.txt", ios::app);
ofs_ << "Total Memery: " << total << endl;
ofs_ << "Used Memery : " << used << endl;
ofs_ << "Free Memery : " << free + buffers + cached - shared << endl;
ofs_.close();
// Todo: Dont't forget delete the middle file named temp.txt
system("rm -rf temp.txt");
}
}
return 0;
}
结果