zoukankan      html  css  js  c++  java
  • [转]VC++获取文件大小集锦

    方法一:  
       
    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
    ");  
     return 4;  
     }  
     dwFileSize = GetFileSize(hFile, NULL);  
     printf("hFile size: %10d
    ", 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
    ",getfilesize());  
         printf("getfilesize01()=%d
    ",getfilesize01());  
         printf("getfilesize02()=%d
    ",getfilesize02());  
         printf("getfilesize03()=%d
    ",getfilesize03());  
         printf("getfilesize04()=%d
    ",getfilesize04());  
         printf("getfilesize05()=%d
    ",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;  
     }  
  • 相关阅读:
    显而易见的python
    GitHub 使用教程图文详解
    linux下搭建hexo环境
    linux 删除软链接
    Django2.1入门教程
    windows下安装PyQt4
    python3 Flask安装
    学习之源
    白话C++系列教程
    面试笔试试题精选
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/8395188.html
Copyright © 2011-2022 走看看