http://blog.163.com/chi.139@126/blog/static/778267772010627112031918/
不想用Linux 的命令获得磁盘的大小,而是用函数得到磁盘的信息,那么我们就需要用到下面二个函数其中的一种了:
头文件: #include <sys/vfs.h> /* or <sys/statfs.h> */
int statfs(const char *path, struct statfs *buf);
int fstatfs(int fd, struct statfs *buf);
其中 struct statfs
{
long f_type; /* type of filesystem (see below) */
long f_bsize; /* optimal transfer block size */
long f_blocks; /* total data blocks in file system */
long f_bfree; /* free blocks in fs */
long f_bavail; /* free blocks avail to non-superuser */
long f_files; /* total file nodes in file system */
long f_ffree; /* free file nodes in fs */
fsid_t f_fsid; /* file system id */
long f_namelen; /* maximum length of filenames */
};
大家可以参考网页:http://www.tin.org/bin/man.cgi?section=2&topic=statfs
我这里是用的第一个函数实现的,给大家帖出一个示例来供大家参考:
C/C++ code:
#include <sys/vfs.h>
#include <iostream>
using namespace std;
int main() {
struct statfs diskInfo;
statfs(path,&diskInfo);
unsigned long long totalBlocks = diskInfo.f_bsize;
unsigned long long totalSize = totalBlocks * diskInfo.f_blocks;
printf("TOTAL_SIZE == %ld MB\n",totalSize>>20);
unsigned long long freeDisk = diskInfo.f_bfree*totalBlocks;
printf("DISK_FREE == %ld MB\n",freeDisk>>20);
return freeDisk>>20;
}
#include <stdio.h>
#include <sys/vfs.h>
int main()
{
struct statfs buf;
statfs("/dev/sda1",&buf);
printf("%d",buf.f_type);`
}
CPU查看:
1、 # dmesg | grep CPU
Initializing CPU#0
CPU: Trace cache: 12K uops
CPU: L1 I-cache: 0K, L1 D-cache: 16K
CPU: L2 cache: 256K
Intel machine check reporting enabled on CPU#0.
CPU: After generic, caps: bfebfbff 00000000 00000000 00000000
CPU: Common caps: bfebfbff 00000000 00000000 00000000
CPU: Intel(R) Celeron(R) CPU 2.53GHz stepping 01
microcode: CPU0 updated from revision 0x0 to 0x12, date = 11222004
2、 # cat /proc/cpuinfo | grep name
model name : Intel(R) Celeron(R) CPU 2.53GHz
3、 # rpm -q --queryformat "%{ARCH}\n" kernel
i686
内存:# cat /proc/meminfo
硬盘:# df命令查看大小
#hdparm -i /dev/hda查看型号
4、dmidecode查看计算机的硬件信息(包括主板等)