linux操作系统下用C++读取arm的内核信息和内存CPU信息

下面通过一个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;
}

 

 

结果

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值