推荐的方法:
#include <sys/stat.h> unsigned long get_file_size(const char *pPath) { unsigned long nFileSize = -1; struct stat statbuff; if (stat(pPath, &statbuff) >= 0) { nFileSize = statbuff.st_size; } return nFileSize; }
另外一种方法:
unsigned long get_file_size2(const char *pPath) { unsigned long nFileSize = -1; FILE *pFile; pFile = fopen(pPath, "r"); if(pFile) { fseek(pFile, 0L, SEEK_END); nFileSize = ftell(pFile); fclose(pFile); } return nFileSize; }