CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2005
    Posts
    140

    Getting filesize using GetFileSizeEx

    Hi,
    I am trying to get the file size using GetFileSizeEx function . howver i am getting exception at that point. Please tell me what i am doing wrong.

    Also please tell me how to convert the return value to size in Megabyte.
    Code:
    HANDLE hFile = CreateFile(str.GetBuffer(), 0, 0, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    		str.ReleaseBuffer();
    		PLARGE_INTEGER pInteger=NULL;
    		if (hFile != INVALID_HANDLE_VALUE)
    		  {
           BOOL b= GetFileSizeEx(hFile, pInteger);// Excption here.
            
            CloseHandle(hFile);
         }

    Regards,
    John.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Getting filesize using GetFileSizeEx

    You code doesn't compile. So what "exception" do you mean?

    // Edit: btw, did you read the GetFileSizeEx documentation in MSDN?
    Last edited by VictorN; November 11th, 2010 at 02:16 AM.
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Getting filesize using GetFileSizeEx

    You might think of using stat function instead. It is much easier and doesn't need an open of the file:

    Code:
    #include <sys/stat.h>
    
    ...
         struct stat fs = { 0 };
         if (stat((const char *)str, &fs) == 0)
         {
               size_t file_size = fs.st_size;
               ...
         }

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Getting filesize using GetFileSizeEx

    Quote Originally Posted by reachb4 View Post
    Hi,
    I am trying to get the file size using GetFileSizeEx function . howver i am getting exception at that point. Please tell me what i am doing wrong.

    Also please tell me how to convert the return value to size in Megabyte.
    Code:
    HANDLE hFile = CreateFile(str.GetBuffer(), 0, 0, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    		str.ReleaseBuffer();
    		PLARGE_INTEGER pInteger=NULL;
    		if (hFile != INVALID_HANDLE_VALUE)
    		  {
           BOOL b= GetFileSizeEx(hFile, pInteger);// Excption here.
            
            CloseHandle(hFile);
         }

    Regards,
    John.
    The pInteger must not be NULL. It must point to a LARGE_INTEGER where the size value could be stored into. It now crashes cause you passed a NULL pointer.


    Code:
    	
    LARGE_INTEGER filesize = 0;
    if (hFile != INVALID_HANDLE_VALUE)
    {
        BOOL b= GetFileSizeEx(hFile, &filesize);

  5. #5
    Join Date
    Mar 2005
    Posts
    140

    Re: Getting filesize using GetFileSizeEx

    Thank you all i find the resolution from the link.

    [link]http://blog.kowalczyk.info/article/Get-file-size-under-windows.html[/link]
    Code:
    long GetFileSize(const TCHAR *fileName)
    {
        BOOL                        fOk;
        WIN32_FILE_ATTRIBUTE_DATA   fileInfo;
    
        if (NULL == fileName)
            return -1;
    
        fOk = GetFileAttributesEx(fileName, GetFileExInfoStandard, (void*)&fileInfo);
        if (!fOk)
            return -1;
        assert(0 == fileInfo.nFileSizeHigh);
        return (long)fileInfo.nFileSizeLow;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured