關於部落格
研究所升學、程式語言資訊、C/C++、Python、Perl、Shell、MySQL、PHP、CSS、VBA、JavaScript
  • 81462

    累積人氣

  • 1

    今日人氣

    0

    追蹤人氣

取得檔案大小~

<方法一> 利用 ftell

unsigned long  GetFileLength ( FILE * fileName)
{
     unsigned long pos = ftell(fileName);
     unsigned long len = 0;
   
     fseek ( fileName, 0L, SEEK_END );
     len = ftell ( fileName );
     fseek ( fileName, pos, SEEK_SET );
     return len;
}

<方法二>利用stat

unsigned long  GetFileLength2 ( char * fileName)

{
    struct stat buf;
    int i = stat ( fileName, &buf );

    if (i !=0)
       MessageBox(NULL,"ERROR for STAT","ERROR",0);

    return buf.st_size;
}

上面方法來自於 http://www.tek-tips.com/viewthread.cfm?qid=1080801&page=6 

<方法三>利用filelength 必須include< fcntl.h>

原型: long filelength(int fd)

long GetFileLength3(char *fileName)

{

        int fd = open(fileName,O_RDONLY | O_BINARY);

        if(fd == -1 ) return -1;

        long lsize = filelength(fd);

        close(fd);

        return lsize;

}

<方法四> 利用Windows API GetFileSize & GetFileSizeEx

範例參考http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfilesize.asp

Large File Size 請使用 GetFileSizeEx函式。

<方法五>利用Windows API  FindFirstFile()

/// For Windows:
#include <windows.h>
double dblFileSize(const char* fname)
{
  if (!fname && !*fname)
     return 0.0;
  HANDLE h;
  WIN32_FIND_DATA info;

  if ((h=FindFirstFile(fname,&info)) 
      != INVALID_HANDLE_VALUE)
  {
     FindClose(h);
     if ((info.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
         == 0) // Is it a file?
     {
        union
        {
          struct { DWORD low, high; } lh;
          __int64 size; // MS large int extension
        } file;
        file.lh.low = info.nFileSizeLow;
        file.lh.high= info.nFileSizeHigh;
        return file.size; // will be casted to double
     }
     // It's a directory, not a file
  }
  return 0.0; // No such name.
}

<方法五>來自於 http://www.tek-tips.com/viewthread.cfm?qid=1080801&page=6 

方法 1~3都無法處理大檔案,要注意~ : )

資料來源 : http://tw.myblog.yahoo.com/josh-chang/article?mid=94&prev=95&next=58&l=f&fid=8

相簿設定
標籤設定
相簿狀態