Is there a function that returns how much space is free on a drive partition given a directory path?
Is there a function that returns how much space is free on a drive partition given a directory path?
You can use 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);
Example:
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
space_info si = space(".");
cout << si.available << endl;
Returns: An object of type space_info. The value of the space_info object is determined as if by using POSIX statvfs() to obtain a POSIX struct statvfs, and then multiplying its f_blocks, f_bfree, and f_bavail members by its f_frsize member, and assigning the results to the capacity, free, and available members respectively. Any members for which the value cannot be determined shall be set to -1.
You can use 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';
}
You can use Qt class QStorageInfo to acquire hardisk free space: First,you should include the header:
#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;
}