在linux中,如何用程序查看剩余硬盘大小
我想在程序中编写一个函数,函数的返回值是硬盘的剩余空间大小,该如何实现?
我本想用system("df /dev/had1");但是这样结果仅仅是在屏幕上输出,而不能赋给变量
并且df /dev/hda1得到的信息太多,我只需要 可用 那一项就可以了
Filesystem   1k-块      已用     可用     已用%  挂载点
/dev/hda1    12890000  aaaa    bbbb    c%     /
------解决方案--------------------你在system("df   /dev/had1")之前把你的“0”文件描述符定位到一个指定文件中,然后再把执行system("df   /dev/had1")。这样输出内容就到了一个指定文件了,你再把指定的行找出来不就行了。
------解决方案--------------------       int fstatvfs(int fildes, struct statvfs *buf);
      int statvfs(const char *restrict path, struct statvfs *restrict buf);
      #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 */
             };
这几个函数都可以
------解决方案--------------------system("echo $(df /dev/had1 | awk 'NR==2{print $4}')")
------解决方案--------------------1、popen("df   /dev/had1");
获取df命令的输出结果分析
2、看df的源码
其实就是读取/etc/mtab并结合statfs/fstatfs来做的