方法一: 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 iresult; struct _stat buf; iresult = _stat(__FILE__,&buf); if(iresult == 0) { return buf.st_size; } return NULL; } int getfilesize01() { int fp; fp=_open(__FILE__,_O_RDONLY); if(fp==-1) return NULL; return _filelength(fp); //return NULL; } int getfilesize02() { int fp; fp=_open(__FILE__,_O_RDONLY); if(fp==-1) return NULL; return _lseek(fp,0,SEEK_END); //return NULL; } int getfilesize03() { int fp; fp=_open(__FILE__,_O_RDONLY); if(fp==-1) return NULL; return _lseek(fp,0,SEEK_END); //return NULL; } int getfilesize04() { FILE *fp; if((fp=fopen(__FILE__,"r"))==NULL) return 0; fseek(fp,0,SEEK_END); return ftell(fp); //return NULL; } int getfilesize05() { FILE *fp; char str[1]; if((fp=fopen(__FILE__,"rb"))==NULL) return 0; for(int i = 0;!feof(fp);i++) { fread(&str,1,1,fp); } return i - 1; //return NULL; } int main(int argc, char* argv[]) { printf("getfilesize()=%d\n",getfilesize()); printf("getfilesize01()=%d\n",getfilesize01()); printf("getfilesize02()=%d\n",getfilesize02()); printf("getfilesize03()=%d\n",getfilesize03()); printf("getfilesize04()=%d\n",getfilesize04()); printf("getfilesize05()=%d\n",getfilesize05()); return 0; } ************************************************************************************** VC获取文件夹大小代码: 以下是代码片段: DWORD64 GetFolderSize(LPCTSTR szPath, DWORD *dwFiles, DWORD *dwFolders) { TCHAR szFileFilter[512]; TCHAR szFilePath[512]; HANDLE hFind = NULL; WIN32_FIND_DATA fileinfo; DWORD64 dwSize = 0; strcpy(szFilePath,szPath); strcat(szFilePath,"\\"); strcpy(szFileFilter,szFilePath); strcat(szFileFilter,"*.*"); hFind = FindFirstFile(szFileFilter,&fileinfo); do { if(fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (!strcmp(fileinfo.cFileName,".") || !strcmp(fileinfo.cFileName,"..")) { //Do nothing for "." and ".." folders } else { TCHAR sztmp[512]; strcpy(sztmp,szFilePath); strcat(sztmp,fileinfo.cFileName); dwSize = dwSize + GetFolderSize(sztmp); if(dwFolders != NULL) { ++(*dwFolders); } } } else { if(dwFiles != NULL) { ++(*dwFiles); } } dwSize += fileinfo.nFileSizeLow; }while(FindNextFile(hFind,&fileinfo)); FindClose(hFind); return dwSize; }