tags:

views:

157

answers:

3

I need to get the size of files. These files range in size from very small to 10's of gigabytes. Are there any c functions that work in both linux and windows to do this? I've seen fileinfo64 but that seems to be windows only and i haven't been able to determine the maximum size supported by stat.

A: 

Duplicate: http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c

Mark
The question and answers provided do not take into account large files. This question is specifically asking for the possibility of a cross-platform function that can be used to determine the size of potentially large files.
dreamlax
+3  A: 

Linux provides the stat64 call which should be the equivalent of fileinfo64. I would simply use a #ifdef to select the correct one.

In fact, a cursory google seems to indicate that stat64 is also available in Windows so it may be easier than you think.

paxdiablo
does this also work in windows?
bobbyb
+2  A: 

In Windows, you can use GetFileSize(). The first parameter to this function is a handle to the file, and the second parameter takes a reference to a DWORD, which upon return will contain the high 32-bits of the file's size. The function returns the low 32-bits of the file's size.

For example

DWORD dwFileSizeLow;
DWORD dwFileSizeHigh;
HANDLE hFile;

hFile = CreateFile (/* Enter the thousands of parameters required here */);

if (hFile != INVALID_HANDLE_VALUE)
    dwFileSizeLow = GetFileSize (hFile, &dwFileSizeHigh);

unsigned __int64 fullSize = (((__int64) dwFileSizeHigh) << 32) | dwFileSizeLow
dreamlax