Howto get hardware information in Linux using C++

前端 未结 5 1861
广开言路
广开言路 2021-02-14 17:01

I need to get specifications of hard disk on both Win and *nix machines. I used on Linux like this:

   static struct hd_driveid hd;
         


        
5条回答
  •  南方客
    南方客 (楼主)
    2021-02-14 17:50

    //Piece of code working for me with Boost LIB usage
    //-----------------------------------------------------
    #include 
    #include 
    //---    
    using namespace boost::filesystem;
    //---
    struct sysinfo info;
    sysinfo( &info );
    //---
    space_info si = space(".");
    //---
    unsigned num_cpu = std::thread::hardware_concurrency();
    //---
    ifstream  cpu_freq("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq");
    ifstream cpu_temp("/sys/class/thermal/thermal_zone0/temp");
    //---
    std::string cpunumber = to_string(num_cpu);
    std::string cpufrequency = cpu_freq.str();
    std::string cputemp = cpu_temp.str();
    std::string mem_size = to_string( (size_t)info.totalram *     (size_t)info.mem_unit );
    std::string disk_available = to_string(si.available);
    std::string fslevel = to_string( (si.available/si.capacity)*100 );
    //---
    

提交回复
热议问题