**************************************************************************************
方法一:
WIN32_FIND_DATA fileInfo;
HANDLE hFind;
DWORD fileSize;
const char *fileName = 文件的路径及名字;
hFind = FindFirstFile(fileName ,&fileInfo);
if(hFind != INVALID_HANDLE_VALUE)
fileSize = fileInfo.nFileSizeLow;
FindClose(hFind);
方法二:
HANDLE hFile; // the file handle
DWORD dwFileSize; // temporary storage for file sizes
// Create the test file. Open it "Create Always" to overwrite any
// existing file. The data is re-created below.
hFile = CreateFile(lpcTheFile, GENERIC_READ | GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("hFile is NULL\n");
return 4;
}
dwFileSize = GetFileSize(hFile, NULL);
printf("hFile size: %10d\n", dwFileSize);
**************************************************************************************
C和C++获取文件大小的方法总结
2008年03月31日 星期一 14:21
1.MFC中的方法:(C++)
CFileStatus status;
CFile::GetStatus("D:\\test.txt",status);
long lSizeOfFile;
lSizeOfFile = status.m_size;
lSizeOfFile的值就是D:\\test.txt文件的大小
2.标准C获得文件大小的5种方法
(注意:"__FILE__"指的是当前文件,你可以改为有效路径的目标文件,比如"D:\\test.txt")
#include "stdafx.h"
#include "stdio.h"
#include <sys/stat.h>
#include <io.h>
#include <FCNTL.H>
int getfilesize()
{
}
int getfilesize01()
{
}
int getfilesize02()
{
}
int getfilesize03()
{
}
int getfilesize04()
{
}
int getfilesize05()
{
}
int main(int argc, char* argv[])
{
}
**************************************************************************************
VC获取文件夹大小代码:
以下是代码片段:
DWORD64 GetFolderSize(LPCTSTR szPath, DWORD *dwFiles, DWORD *dwFolders)
{
TCHAR szFileFilter[512];
TCHAR szFilePath[512];
HANDLE hFind = NULL;
WIN32_FIND_DATA fileinfo;
DWORD64
strcpy(szFilePath,szPath);
strcat(szFilePath,"\");
strcpy(szFileFilter,szFilePath);
strcat(szFileFilter,"*.*");
hFind = FindFirstFile(szFileFilter,&fileinfo);
do
{
}while(FindNextFile(hFind,&fileinfo));
FindClose(hFind);
return dwSize;
}