我需要在Win和* nix机器上获得硬盘规格。我在Linux上使用<hdreg.h>
就像这样:
static struct hd_driveid hd;
int device;
if ((device = open("/dev/sda", O_RDONLY | O_NONBLOCK)) < 0)
{
cerr << "ERROR: Cannot open device /dev/sda \n";
exit(1);
}
if (!ioctl(device, HDIO_GET_IDENTITY, &hd))
{
cout << hd.model << endl;
cout << hd.serial_no << endl;
cout << hd.heads << endl;
}
我需要hd_driveid
告诉我有关磁盘的更多信息。我想知道:
我的问题是:
答案 0 :(得分:9)
列表中几乎所有内容都与“硬盘规格”无关:
hd_driveid.sector_bytes
获得(通常为512,但有些现代驱动器使用4096)。我不知道最大的“块大小”,这是文件系统的属性。我也不确定为什么这很有用。行业的总大小在hd_driveid.lba_capacity_2
。此外,可以通过类似
#define _FILE_OFFSET_BITS 64
#include <sys/types.h>
#include <unistd.h>
...
off_t size_in_bytes = lseek(device, 0, SEEK_END);
if (size_in_bytes == (off_t)-1) { ... error, error code in ERRNO ... }
请注意,在这两种情况下,它可能比C×H×S计算的尺寸大几兆字。
如果您告诉我们您想要这些信息的原因,可能会有所帮助......
答案 1 :(得分:3)
不,没有与平台无关的方式。甚至没有* nix的方式。只有Linux方式。
在Linux中,/proc
文件系统中的各种文件中都提供了所有相关信息。 /proc/devices
将告诉您哪些设备(/dev/
中的文件可能存在,即使设备不可用,但在这种情况下打开它们会失败),/proc/partitions
会告诉您每个磁盘上有哪些分区,而且您必须在各个子目录中查找信息。只需浏览一下你所需要的Linux系统。
答案 2 :(得分:2)
对于GNU / Linux,请看一下:obtaining hard disk metadata
答案 3 :(得分:2)
//-------------------------------------------------
// Without Boost LIB usage
//-------------------------------------------------
#include <sys/statvfs.h>
#include <sys/sysinfo.h>
//-------------------------------------------------
stringstream strStream;
unsigned long hdd_size;
unsigned long hdd_free;
ostringstream strConvert;
//---
struct sysinfo info;
sysinfo( &info );
//---
struct statvfs fsinfo;
statvfs("/", &fsinfo);
//---
//---
unsigned num_cpu = std::thread::hardware_concurrency();
//---
ifstream cpu_freq("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq");
strStream << cpu_freq.rdbuf();
std::string cpufrequency = strStream.str();
//---
strStream.str("");
ifstream cpu_temp("/sys/class/thermal/thermal_zone0/temp");
strStream << cpu_temp.rdbuf();
strConvert<< fixed << setprecision(2) << std::stof(strStream.str());
std::string cputemp = strConvert.str();
//---
std::string mem_size = to_string( (size_t)info.totalram * (size_t)info.mem_unit );
//---
hdd_size = fsinfo.f_frsize * fsinfo.f_blocks;
hdd_free = fsinfo.f_bsize * fsinfo.f_bfree;
//---
std::cout << "CPU core number ==" << num_cpu << endl;
std::cout << "CPU core speed ==" << cpufrequency << endl;
std::cout << "CPU temperature (C) ==" << cputemp << endl;
//---
std::cout << "Memory size ==" << mem_size << endl;
//---
std::cout << "Disk, filesystem size ==" << hdd_size << endl;
std::cout << "Disk free space ==" << hdd_free << endl;
//---
答案 4 :(得分:1)
//Piece of code working for me with Boost LIB usage
//-----------------------------------------------------
#include <sys/sysinfo.h>
#include <boost/filesystem.hpp>
//---
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 );
//---