boost、Windows、Linux获取磁盘总量、剩余磁盘空间

boost库获取指定路径剩余磁盘空间

为啥使用boost库?

方便,真的方便!后面还介绍windows和linux下获取方式

## boost库准备,自我准备
## 本人boost库路径: /usr/local/boost1770/lib
##      头文件路径: /usr/local/boost1770/include
## 
## boost::filesystem::space文档
## https://www.boost.org/doc/libs/1_76_0/libs/filesystem/doc/reference.html#space

示例代码

/**test.cpp*/
#include 
#include 

int main()
{
    boost::filesystem::space_info si = boost::filesystem::space("/root");
    std::cout << si.capacity << std::endl;    // byte
    std::cout << si.available << std::endl;   // byte
    std::cout << si.free << std::endl;        // byte
    return 0;
}

编译&执行

export LD_LIBRARY_PATH=${YOUR_BOOST_LIB_PATH}
g++ -std=c++11 -L${YOUR_BOOST_LIB_PATH} -lboost_atomic -lboost_filesystem -I${YOUR_BOOST_INCLUDE_PATH} test.cpp -o test
./test

Windows版本

函数:GetDiskFreeSpace & GetDiskFreeSpaceEx(GetDiskFreeSpaceExA)

示例代码

#include 
#include 

int main()
{
	DWORD dwTotalClusters;//总的簇
	DWORD dwFreeClusters;//可用的簇
	DWORD dwSectPerClust;//每个簇有多少扇区
	DWORD dwBytesPerSect;//每个扇区多少字节
	BOOL bResult = GetDiskFreeSpace(TEXT("F:/"), &dwSectPerClust, &dwBytesPerSect, &dwFreeClusters, &dwTotalClusters);
	if (bResult) {
		std::cout << "总簇:" << dwTotalClusters << std::endl;
		std::cout << "可用簇:" << dwFreeClusters << std::endl;
		std::cout << "每个簇多少扇区:" << dwSectPerClust << std::endl;
		std::cout << "每个扇区多少字节:" << dwBytesPerSect << std::endl;
		std::cout << "磁盘容量:" << dwTotalClusters * dwSectPerClust*dwBytesPerSect << std::endl;
		std::cout << "可用容量:" << dwFreeClusters *dwSectPerClust*dwBytesPerSect << std::endl;
	}
	std::cout << std::endl << std::endl;

	DWORD64 qwFreeBytes, qwFreeBytesToCaller, qwTotalBytes;
	bResult = GetDiskFreeSpaceEx(TEXT("F:/"),
		(PULARGE_INTEGER)&qwFreeBytesToCaller,
		(PULARGE_INTEGER)&qwTotalBytes,
		(PULARGE_INTEGER)&qwFreeBytes);
	if (bResult){
		std::cout << "磁盘容量:" << qwTotalBytes << std::endl;
		std::cout << "可用容量:" << qwFreeBytes << std::endl;
		std::cout << "空闲容量:" << qwFreeBytesToCaller << std::endl;
	}
	return 0;
}

参考:https://blog.csdn.net/x_y_q_/article/details/52709474

LINUX版本

函数:statvfs()

示例代码

/**
 * space_info space(const path& p);
 * space_info space(const path& p, system::error_code& ec);
 * Returns: An object of type space_info. The value of the space_info object is determined as if by using ISO/IEC 9945 statvfs() to obtain an ISO/IEC 9945 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.
*/
// test.cpp

#include 
#include 

int main()
{
    struct statvfs buf;
    statvfs("/root", &buf);
    std::cout << "磁盘总量:" << buf.f_frsize * buf.f_blocks << std::endl;
    std::cout << "磁盘空余:" << buf.f_frsize * buf.f_bfree << std::endl;
    std::cout << "磁盘空余:(for unprivileged users)" << buf.f_frsize * buf.f_bavail << std::endl;
    return 0;
}

编译&执行

g++ -std=c++11 test.cpp -o test
./test

你可能感兴趣的:(杂项,linux,windows)