czwartek, 20 grudnia 2018

Use "/proc/cpuinfo" C++ (g++)

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

class CPU
{
    private:
    FILE *fp1,*fp2,*fp3;
    char buff1[2048],buff2[2048],buff3[2048];
    char *conv1,*conv2,*conv3,*name;
    float speed_cpu,memory_cache;
    size_t file1_read,file2_read,file3_read;
    public:
   
    void open_Files();
    void name_Info();
    void speed_Info();
    void cache_size_Info();
};
void CPU::open_Files()
{

    fp1=fopen("/proc/cpuinfo","r");
    file1_read=fread(buff1,1,sizeof(buff1),fp1);
    fclose(fp1);
    fp2=fopen("/proc/cpuinfo","r");
    file2_read=fread(buff2,1,sizeof(buff2),fp2);
    fclose(fp2);
    fp3=fopen("/proc/cpuinfo","r");
    file3_read=fread(buff3,1,sizeof(buff3),fp3);
    fclose(fp3);
   
    buff1[file1_read]='\0';
    buff2[file2_read]='\0';
    buff3[file3_read]='\0';
   
   
}
void CPU::name_Info()
{
    conv1=strstr(buff1,"model name");
    sscanf(conv1,"model name : %s",name);
    cout<<"Model name: "<<name<<endl;
}
void CPU::speed_Info()
{
    conv2=strstr(buff2,"cpu MHz");
    sscanf(conv2,"cpu MHz : %f",&speed_cpu);
    cout<<"cpu MHz: "<<speed_cpu<<endl;
}
void CPU::cache_size_Info()
{
    conv3=strstr(buff3,"cache size");
    sscanf(conv3,"cache size: %f",&memory_cache);
    cout<<"Cache size (KB): "<<memory_cache<<endl;
}


int main(int argc, char **argv)
{
    CPU *info_CPU=new class CPU;
    info_CPU->open_Files();
    info_CPU->name_Info();
    info_CPU->speed_Info();
    info_CPU->cache_size_Info();
    delete info_CPU;
    return 0;
}

Brak komentarzy:

Prześlij komentarz