是否有一种函数可以返回给定目录路径下驱动器分区上可用空间的大小?
查看man statvfs(2)
。
我认为你可以将“可用空间”计算为f_bsize * f_bfree
。
NAME
statvfs, fstatvfs - get file system statistics
SYNOPSIS
#include <sys/statvfs.h>
int statvfs(const char *path, struct statvfs *buf);
int fstatvfs(int fd, struct statvfs *buf);
DESCRIPTION
The function statvfs() returns information about a mounted file system.
path is the pathname of any file within the mounted file system. buf
is a pointer to a statvfs structure defined approximately as follows:
struct statvfs {
unsigned long f_bsize; /* file system block size */
unsigned long f_frsize; /* fragment size */
fsblkcnt_t f_blocks; /* size of fs in f_frsize units */
fsblkcnt_t f_bfree; /* # free blocks */
fsblkcnt_t f_bavail; /* # free blocks for unprivileged users */
fsfilcnt_t f_files; /* # inodes */
fsfilcnt_t f_ffree; /* # free inodes */
fsfilcnt_t f_favail; /* # free inodes for unprivileged users */
unsigned long f_fsid; /* file system ID */
unsigned long f_flag; /* mount flags */
unsigned long f_namemax; /* maximum filename length */
};
你可以使用 boost::filesystem 库:
struct space_info // returned by space function
{
uintmax_t capacity;
uintmax_t free;
uintmax_t available; // free space available to a non-privileged process
};
space_info space(const path& p);
space_info space(const path& p, system::error_code& ec);
例子:
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
space_info si = space(".");
cout << si.available << endl;
返回:一个space_info类型的对象。space_info对象的值确定方式类似于使用POSIX statvfs()获取POSIX结构体statvfs,然后将其f_blocks、f_bfree和f_bavail成员分别乘以其f_frsize成员,并将结果分配给容量、空闲和可用成员。任何无法确定值的成员都将设置为-1。您可以使用std::filesystem::space
:
#include <iostream> // only needed for screen output
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::space_info tmp = fs::space("/tmp");
std::cout << "Free space: " << tmp.free << '\n'
<< "Available space: " << tmp.available << '\n';
}
#include <QStorageInfo>
#define GB (1024 * 1024 * 1024)
bool CheckHardiskFree(const QString &strDisk)
{
QStorageInfo storage(strDisk);
if(storage.isValid() && storage.isReady())
{
double useGb =(storage.bytesTotal()-storage.bytesAvailable()) * 1.0/ GB;
double freeGb =storage.bytesAvailable() * 1.0 / GB;
double allGb =storage.bytesTotal()* 1.0 / GB;
return true;
}
return false;
}
可以通过使用管道将命令的输出传递到程序中,像这样:
char cmd[]="df -h /path/to/directory" ;
FILE* apipe = popen(cmd, "r");
// if the popen succeeds read the commands output into the program with
while ( fgets( line, 132 , apipe) )
{ // handle the readed lines
}
pclose(apipe);
// -----------------------------------
在所有文件系统上,返回的结构体的所有成员是否具有有意义的值都是未指定的。
因此,在这种情况下可能不支持FAT32。 - John Ledbetterf_bsize * f_bavail
以使数据与df -h
命令保持一致。 - speed488tune2fs
命令并使用选项-r <count>
或-m <percentage>
来更改此值。在文件系统创建期间也可以进行设置。 - zoke