VC++实现获取文件占用空间大小的两种方法(非文件大小)
// GetFileSpaceSize.cpp : Defines the entry point for the console application.
//
/************************************************************************
* author: HwangBae
* created: 2012/07/21
* Blog: http://hwangbae.cnblogs.com/
* Email: hwangbae@live.cn
************************************************************************/ #include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <math.h> int _tmain(int argc, _TCHAR* argv[])
{
if (argc < )
{
_tprintf_s(_T("Usage: GetFileSpaceSize filename\n"));
return -;
} // 文件路径
LPCTSTR szFileName = argv[]; // 打开文件句柄
HANDLE hFile = ::CreateFile(szFileName, GENERIC_READ | FILE_SHARE_READ, ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
_tprintf_s(_T("Failed to create file handle: %s ! error code:%d\n"), szFileName, GetLastError());
return -;
} // 获取文件大小
UINT64 uFileSize = ;
::GetFileSizeEx(hFile, reinterpret_cast<PLARGE_INTEGER>(&uFileSize));
::CloseHandle(hFile); // 获取磁盘根路径
TCHAR szVolumePathName[] = _T("C:\\");
::GetVolumePathName(szFileName, szVolumePathName, sizeof(szVolumePathName) / sizeof(TCHAR)); // 保存簇信息的变量
DWORD dwSectorsPerCluster = ;
DWORD dwBytesPerSector = ;
DWORD dwNumberOfFreeClusters = ;
DWORD dwTotalNumberOfClusters = ; // 获取簇信息
if (!::GetDiskFreeSpace(
szVolumePathName, //磁盘根路径
&dwSectorsPerCluster, //每簇的扇区数
&dwBytesPerSector, //每扇区的字节数
&dwNumberOfFreeClusters, //空余簇的数量
&dwTotalNumberOfClusters //全部簇的数量
)
)
{
_tprintf_s(_T("Failed to get disk cluster info! error code: %d\n"), GetLastError());
return -;
}
// 簇大小 = 每簇的扇区数 * 每扇区的字节数
DWORD dwClusterSize = dwSectorsPerCluster * dwBytesPerSector; // 计算文件占用空间
// 公式:(以字节为单位)
// 簇数 = 向上取整(文件大小 / 簇大小)
// 占用空间 = 簇数 * 簇大小
UINT64 dwFileSpacesize = static_cast<UINT64>(ceil(uFileSize / static_cast<double>(dwClusterSize)) * dwClusterSize); _tprintf_s(_T("FileName : %s\n"), szFileName);
_tprintf_s(_T("FileSize : %I64u Byte\n"), uFileSize);
_tprintf_s(_T("FileSpacesSize : %I64u Byte\n"), dwFileSpacesize);
return ;
}
方法二:
// GetFileSpaceSize.cpp : Defines the entry point for the console application.
//
/************************************************************************
* author: HwangBae
* created: 2012/07/23
* Blog: http://hwangbae.cnblogs.com/
* Email: hwangbae@live.cn
************************************************************************/ #include <windows.h>
#include <tchar.h>
#include <stdio.h> #define CLOSE_HANDLE(handle) \
do \
{ \
CloseHandle(handle); \
handle = NULL; \
} while (FALSE) int _tmain(int argc, _TCHAR* argv[])
{
if (argc < )
{
_tprintf_s(_T("Usage: GetFileSpaceSize filename\n"));
return -;
} // 文件路径
LPCTSTR szFileName = argv[]; // 打开文件句柄
HANDLE hFile = ::CreateFile(szFileName, GENERIC_READ | FILE_SHARE_READ, ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
_tprintf_s(_T("Failed to create file handle: %s ! error code:%d\n"), szFileName, GetLastError());
return -;
} // 获取文件大小
UINT64 uFileSize = ;
::GetFileSizeEx(hFile, reinterpret_cast<PLARGE_INTEGER>(&uFileSize)); FILE_STANDARD_INFO fsi = {};
if (!::GetFileInformationByHandleEx(hFile, FileStandardInfo, &fsi, sizeof(FILE_STANDARD_INFO)))
{
_tprintf_s(_T("Failed to get file info! error code:%d\n"), GetLastError());
CLOSE_HANDLE(hFile);
return -;
} _tprintf_s(_T("FileName : %s\n"), szFileName);
_tprintf_s(_T("FileSize : %I64u Byte\n"), uFileSize);
_tprintf_s(_T("FileSpacesSize : %I64u Byte\n"), fsi.AllocationSize);
CLOSE_HANDLE(hFile);
return ;
}
转载:http://www.cnblogs.com/hwangbae/archive/2012/07/21/2602592.html
VC++实现获取文件占用空间大小的两种方法(非文件大小)的更多相关文章
- JAVA中获取文件MD5值的四种方法
JAVA中获取文件MD5值的四种方法其实都很类似,因为核心都是通过JAVA自带的MessageDigest类来实现.获取文件MD5值主要分为三个步骤,第一步获取文件的byte信息,第二步通过Messa ...
- PHP获取文件后缀名的三种方法
如下: <? PHP获取文件后缀名的几种方法1: function get_file_type($filename){ $type = substr($filename, strrpos($fi ...
- java 获取键盘输入常用的两种方法
java 获取键盘输入常用的两种方法 方法1: 通过 Scanner Scanner input = new Scanner(System.in); String s = input.nextLine ...
- PHP中获取文件扩展名的N种方法
PHP中获取文件扩展名的N种方法 从网上收罗的,基本上就以下这几种方式: 第1种方法:function get_extension($file){substr(strrchr($file, '.'), ...
- QT中获取选中的radioButton的两种方法(动态取得控件的objectName之后,对名字进行比较)
QT中获取选中的radioButton的两种方法 QT中要获取radioButton组中被选中的那个按钮,可以采用两种如下两种办法进行: 方法一:采用对象名称进行获取 代码: 1 QRadioBu ...
- 使用JavaScript获取URL中的参数(两种方法)
本文给大家分享两种方法使用js获取url中的参数,其中方法二是使用的正则表达式方法,大家可以根据需要选择比较好的方法,废话不多说了,直接看详细介绍吧. 方法一: //取url参数 var type = ...
- QT中获取选中的radioButton的两种方法
QT中要获取radioButton组中被选中的那个按钮,可以采用两种如下两种办法进行: 方法一:采用对象名称进行获取 代码: 1 QRadioButton* pbtn = qobject_cast&l ...
- C#实现Web文件上传的两种方法
1. C#实现Web文件的上传 在Web编程中,我们常需要把一些本地文件上传到Web服务器上,上传后,用户可以通过浏览器方便地浏览这些文件,应用十分广泛. 那么使用C#如何实现文件上传的功能呢?下面笔 ...
- Java读取文件夹大小的6种方法及代码
(一)单线程递归方式 package com.taobao.test; import java.io.File; public class TotalFileSizeSequential { publ ...
随机推荐
- nautilus
在~/.bashrc中定义命令别名,添加以下命令: # some more nautilus aliases alias here='nautilus . > /dev/null 2>&a ...
- Photoshop制作倒影的两种方法
图片加了倒影,画面立刻变得生动起来.而用PS,制作倒影是如此的方便. 素材1 将素材1导入文档,ctrl+J复制图层,编辑-变换-垂直翻转将翻转的图层拖至下方 为翻转的图层添加图层蒙版,选中渐变工具, ...
- 匿名内部类的参数引用只能是final,可能遇到的问题及其解决
这个是我碰到比较多次的问题,一开始是不解,不过查了下大家都觉得没什么,而且只是加个final好像影响也不大,于是我就直接加个final了事,之后也不管了 直到昨天: 遇到了这个宿命般的问题 难道解决方 ...
- HIVE表保存的路径
HIVE表保存的默认路径在${HIVE_HOME}/conf/hive-site.xml配置文件的hive.metastore.warehouse.dir属性指定
- BUG笔记:Firefox select选项右侧边框没了
Firefox 的default select在某些情况下右侧边框会消失.截图如下: 这个目前为止没有看到有任何解决方案,HACK也没有...囧... 有高人知道吗?
- 【Espruino】NO.07 获取电压值
http://blog.csdn.net/qwert1213131/article/details/27985645 本文属于个人理解,能力有限,纰漏在所难免.还望指正! [小鱼有点电] 前几节的内容 ...
- 001-读书笔记-企业IT架构转型之道-阿里巴巴中台战略思想与架构实战-第一章 阿里巴巴集团中台战略引发的思考
1.1.阿里中台发展 组件中台可能问题:组织间业务协作.业务核心能力的沉淀.组织KPI考核等 1.2.企业信息中心发展的症结 1.烟囱式系统建设模式 独立构建独立维护 缺点:1.重复功能建设和维护带来 ...
- 在ASP.NET Web Application中通过SOAP协议调用Bing搜索服务
本文介绍了如何在ASP.NET Web Application中将Bing搜索作为Web Service来使用,并通过HTTP的SOAP协议在ASP.NET Web Application中调用Bin ...
- C# 定时器 一个简单 并且可以直接运行的Demo
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- webdriver鼠标上下滑动
有时候我们需要对窗口显示的页面上下滑动,以显示当前正在处理的位置,这就需要用到webdriver模拟鼠标上下滑动 package test20161201; import org.openqa.sel ...