关于用linux C函数实现查看磁盘空间大小

239 篇文章 2 订阅
147 篇文章 1 订阅

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查看计算机的硬件信息(包括主板等)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值