method One: Win32_find_data fileInfo; HANDLE Hfind; DWORD fileSize; Const Char*filename =the path and name of the file; Hfind= FindFirstFile (FileName,&fileInfo); if(Hfind! =invalid_handle_value) FileSize=Fileinfo.nfilesizelow; FindClose (hfind); Method two: HANDLE hfile; //The file handleDWORD 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 and C++How to get the file size summary Monday, March 31, 2008 -: + 1. Methods in MFC: (C + +) cfilestatus status; CFile::GetStatus ("D:\\test.txt", status); LongLsizeoffile; Lsizeoffile=status.m_size; The value of Lsizeoffile is the size of the D:\\test.txt file25 ways to get file size in standard C (Note:"__file__"Refers to the current file, you can change to a valid path of the target file, such as"D:\\test.txt") #include"stdafx.h"#include"stdio.h"#include<sys/stat.h>#include<io.h>#include<FCNTL.H>intGetFileSize () {intIresult; struct_stat buf; Iresult= _stat (__file__,&buf); if(Iresult = =0) { returnbuf.st_size; } returnNULL; } intgetfilesize01 () {intFP; FP=_open (__file__,_o_rdonly); if(fp==-1) returnNULL; return_filelength (FP); //return NULL; } intgetfilesize02 () {intFP; FP=_open (__file__,_o_rdonly); if(fp==-1) returnNULL; return_lseek (FP,0, Seek_end); //return NULL; } intgetfilesize03 () {intFP; FP=_open (__file__,_o_rdonly); if(fp==-1) returnNULL; return_lseek (FP,0, Seek_end); //return NULL; } intgetfilesize04 () {FILE*FP; if(Fp=fopen (__file__,"R"))==NULL)return 0; Fseek (FP,0, Seek_end); returnFtell (FP);//return NULL; } intgetfilesize05 () {FILE*FP; Charstr[1]; if(Fp=fopen (__file__,"RB"))==NULL)return 0; for(inti =0;! feof (FP); i++) {fread (&STR,1,1, FP); } returnI1;//return NULL; } intMainintargcChar*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 Get folder Size code: Here is the code snippet: DWORD64 getfoldersize (LPCTSTR szpath, DWORD*dwfiles, DWORD *dwfolders) {TCHAR szfilefilter[ +]; TCHAR szfilepath[ +]; 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[ +]; 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); returndwsize; }
Go VC + + Get File size collection