红联Linux门户
Linux帮助

用C++提取Linux的cpu的利用率

发布时间:2017-05-20 11:33:48来源:linux网站作者:Baterforyou
以下是用C++提取Linux的cpu的利用率的代码:
 
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include<iostream>
#include<fstream>
using namespace std;
class A{
public:
//int c;
void display()
{
system("top -n 1 |grep Cpu | cut -d ' ' -f 9 >>cpu.txt");//把读的cpu的利用率放进文本文件
//c=system(top -n 1 |grep Cpu | cut -d ' ' -f 9)'
}
};
int main()
{
A a;
double b;
ofstream outfile;
outfile.open("cpu.txt",ios::out);
a.display();
ifstream infile;
infile.open("cpu.txt",ios::in);
infile>>b;//把文本文件里面的数据读入变量b
cout<<b<<endl;
cout<<"the process is ok!"<<endl;
return 0;
}
class A{
public:
//int c;
void display()
{
system("top -n 1 |grep Cpu | cut -d ' ' -f 9 >>cpu.txt");//把读的cpu的利用率放进文本文件
//c=system(top -n 1 |grep Cpu | cut -d ' ' -f 9)'
}
};
int main()
{
A a;
double b;
ofstream outfile;
outfile.open("cpu.txt",ios::out);
a.display();
ifstream infile;
infile.open("cpu.txt",ios::in);
infile>>b;//把文本文件里面的数据读入变量b
cout<<b<<endl;
cout<<"the process is ok!"<<endl;
return 0;
}
 
本文永久更新地址:http://www.linuxdiyf.com/linux/30926.html