[WinAPI] Cum aflu dimensiunea unui fisier?

Folosire Windows API in programe C/C++ (forum moderat)
Post Reply
User avatar
Silviu Ardelean
Senior
Senior
Posts: 1175
Joined: 12 Jul 2007, 09:22
Judet: Timiş
Location: Timisoara
Contact:

[WinAPI] Cum aflu dimensiunea unui fisier?

Post by Silviu Ardelean » 25 Nov 2010, 18:05

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:
Last edited by Ovidiu Cucu on 27 Nov 2010, 20:04, edited 2 times in total.
Reason: few cosmetics



User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

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

Post by Ovidiu Cucu » 18 Jul 2011, 13:50

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

mesajflaviu
Membru++
Membru++
Posts: 684
Joined: 10 Sep 2008, 21:40
Judet: Cluj

Re: [WinAPI] Cum aflu dimensiunea unui fisier?

Post by mesajflaviu » 12 Feb 2014, 14:49

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.
Last edited by Marius Bancila on 06 Mar 2014, 17:37, edited 1 time in total.
Reason: m_size e de tip ULONGLONG nu DWORD

Post Reply