From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Getting file size on files > 2GB (like 20 GB in size)

I have seen several threads regarding large file sizes, both positioning and reading from, but what about actuall getting and display the file size and the current byte location in the file.  Anything under 2 GB, you can manage okay (and with fancy casting you can get to 4 GB with unsigned int).  But above that threshold, you are locked out.  I have looked into the fgetpos and fsetpos functions, but there is no way to get the byte position (i.e. value) out.  And when casting the functions to a double or unsigned __int64 (in LW/CVI 7.0) you get a number no where near the actual size or position. Any pointer to CVI functions would be great.
 
Also, displaying the value over 4 GB is impossible with the numeric User Interface control, as the control doesn't allow an __int64, just unsigned int and double. I have tried to cast the __int64 into the double, but there is a warning for truncation.  Any ideas?
 
Example (not real code):
unsigned __int64 Pos
 
fseek (fStream, 0L, SEEK_END);
fgetpos (fStream, (fpos_t *) &Pos)
fseek (fStream, 0L, SEEK_SET);
 
printf("File size = %.0f", (double) Pos);
 
Thanks.
0 Kudos
Message 1 of 4
(2,285 Views)

I found that I have to resort to the Windows SDK functions.  So here is a wrapper I made that returns the file size as a real number to show total number of bytes for a file.  I found that another like had given me the basic answer, but I have to resort to file handles.  So, I take no credit for this code, as it is from another post in the forum (with a small change:

#include <windows.h>

double GetLargeFileSize(char *sFileName)
{

  double dSize;

  LARGE_INTEGER lpNumBytes;
  SECURITY_ATTRIBUTES saAttr;
  HANDLE hFileHandle = NULL;

  #define E32 4294967296.0 //2^32
    
  hFileHandle = CreateFile(sFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
                           OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                          
  if (hFileHandle == INVALID_HANDLE_VALUE)
  {
    dSize = -2.0;
  }
  else
  {
    if (GetFileSizeEx(hFileHandle, &lpNumBytes) > 0)
    {
      dSize = lpNumBytes.HighPart*E32 + lpNumBytes.LowPart;
    }
    else
    {
      dSize = -1.0;
    }
    CloseHandle(hFileHandle);
  }
  return dSize;
}

I return the negative umbers as error indicators so the caller knows that it failed in one of two aspects.  Thanks and cheers from the desert.

0 Kudos
Message 2 of 4
(2,271 Views)


@RorySmash wrote:

I found that I have to resort to the Windows SDK functions.  So here is a wrapper I made that returns the file size as a real number to show total number of bytes for a file.  I found that another like had given me the basic answer, but I have to resort to file handles.  So, I take no credit for this code, as it is from another post in the forum (with a small change:

#include <windows.h>

double GetLargeFileSize(char *sFileName)
{

  double dSize;

  LARGE_INTEGER lpNumBytes;
  SECURITY_ATTRIBUTES saAttr;
  HANDLE hFileHandle = NULL;

  #define E32 4294967296.0 //2^32
    
  hFileHandle = CreateFile(sFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
                           OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                          
  if (hFileHandle == INVALID_HANDLE_VALUE)
  {
    dSize = -2.0;
  }
  else
  {
    if (GetFileSizeEx(hFileHandle, &lpNumBytes) > 0)
    {
      dSize = lpNumBytes.HighPart*E32 + lpNumBytes.LowPart;
    }
    else
    {
      dSize = -1.0;
    }
    CloseHandle(hFileHandle);
  }
  return dSize;
}

I return the negative umbers as error indicators so the caller knows that it failed in one of two aspects.  Thanks and cheers from the desert.



First I think this should have been in the LabWindows/CVI board.

Second, it's quite liklely that CVI 8.x adds int64 support just as LabVIEW did with version 8.0.

Rolf Kalbermatter

Rolf Kalbermatter
My Blog
0 Kudos
Message 3 of 4
(2,241 Views)

Sorry,

I thought I did post it in the LabWindows/CVi area. I apologize for the mistake.  Thanks for the help.

0 Kudos
Message 4 of 4
(2,225 Views)