Page 1 of 1

[WinAPI] Cum aflu dimensiunea unui fisier?

Posted: 25 Nov 2010, 18:05
by Silviu Ardelean
Intrebare: Cum aflu dimensiunea unui fisier?

Raspuns: O metoda este folosind functia FindFirstFile.

Exemplu

Code: Select all

ULONGLONG GetFileSize(LPCTSTR szFilePath)
{
   // NOTE: This is a draft example. 
   // Not all tests are performed, e.g. if szFilePath is valid (see FindFirstFile documentation).

   ULONGLONG nSize = 0;
   WIN32_FIND_DATA findData = {0};
   HANDLE hFind = ::FindFirstFile(szFilePath, &findData);
   if(INVALID_HANDLE_VALUE != hFind)
   {
      ULARGE_INTEGER liSize = {findData.nFileSizeLow, findData.nFileSizeHigh};
      nSize = liSize.QuadPart;
      ::FindClose(hFind);
   }
   return nSize;
}
Vezi si:

[WinAPI] Cum aflu dimensiunea unui fisier? (2)

Posted: 18 Jul 2011, 13:50
by Ovidiu Cucu
Raspuns
Pentru aflarea dimensiunii unui fisier se pot utiliza urmatoarele functii WinAPI:
  1. GetFileSize
    Exemplu

    Code: Select all

    DWORD _GetFileSize(LPCTSTR pszFileName, ULONGLONG& nSize)
    {
       DWORD dwRet = NO_ERROR;
       DWORD dwDesiredAccess = 0; // open just to query info
       HANDLE hFile = ::CreateFile(pszFileName,
          dwDesiredAccess, 0, NULL, OPEN_EXISTING,
          0, NULL);
    
       if(INVALID_HANDLE_VALUE != hFile)
       {
          DWORD dwFileSizeHi = 0;
          DWORD dwFileSizeLo = ::GetFileSize(hFile, &dwFileSizeHi);
          if(INVALID_FILE_SIZE == dwFileSizeLo)
          {
             // check if failed or really is a good low-part value
             dwRet = ::GetLastError();
          }
          if(NO_ERROR == dwRet)
          {
             LARGE_INTEGER liSize = {dwFileSizeLo, dwFileSizeHi};
             nSize = liSize.QuadPart; // file size in bytes
          }
          ::CloseHandle(hFile);
       }
       else
       {
          dwRet = ::GetLastError();
       }
       return dwRet; // NO_ERROR if succeeded
    }
  2. GetFileSizeEx
    Exemplu

    Code: Select all

    DWORD _GetFileSizeEx(LPCTSTR pszFileName, ULONGLONG& nSize)
    {
       DWORD dwRet = NO_ERROR;
       DWORD dwDesiredAccess = 0; // open just to query info
       HANDLE hFile = ::CreateFile(pszFileName,
          dwDesiredAccess, 0, NULL, OPEN_EXISTING,
          0, NULL);
    
       if(INVALID_HANDLE_VALUE != hFile)
       {
          LARGE_INTEGER liSize = {0};
          if(::GetFileSizeEx(hFile, &liSize))
             nSize = liSize.QuadPart; // file size in bytes
          else
             dwRet = ::GetLastError();
          ::CloseHandle(hFile);
       }
       else
       {
          dwRet = ::GetLastError();
       }
       return dwRet; // NO_ERROR if succeeded
    }
  3. FindFirstFile
    Vezi postul precedent.
Resurse
<< Back to Windows API Index

Re: [WinAPI] Cum aflu dimensiunea unui fisier?

Posted: 12 Feb 2014, 14:49
by mesajflaviu
O alta metoda de a afla dimensiunea unui fisier fara a-l deschide, este folosind metoda CFile::GetStatus, versiunea statica.
Exemplu:

Code: Select all

	CString sPath(_T("D:\\Temp\\myfile.txt"));
	CFileStatus status;
	CFile::GetStatus(sPath, status);
	ULONGLONG dwFileSize = status.m_size;
Mai mult, cu ajutorul acestei metode putem afla pe langa marimea fisierului, informatii cu privire la data crearii/modificarii/accesarii, precum si atributele fisierului.