Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the file-size without opening file in C++?

I'm trying to get the filesize of a large file (12gb+) and I don't want to open the file to do so as I assume this would eat a lot of resources. Is there any good API to do so with? I'm in a Windows environment.

like image 261
user1167566 Avatar asked Jan 24 '12 17:01

user1167566


People also ask

How do you determine the size of a file in C?

Using stat() function The stat() function takes the file path and returns a structure containing information about the file pointed by it. To get the size of the file in bytes, use the st_size field of the returned structure.

How do I read file size?

Locate the file or folder whose size you would like to view. Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.

What is St_size in C?

The st_size field gives the size of the file (if it is a regular file or a symbolic link) in bytes. The size of a symbolic link is the length of the pathname it contains, without a terminating null byte. The st_blocks field indicates the number of blocks allocated to the file, 512-byte units.

How do I check the size of a file in C++?

To get a file's size in C++ first open the file and seek it to the end. tell() will tell us the current position of the stream, which will be the number of bytes in the file.


1 Answers

You should call GetFileSizeEx which is easier to use than the older GetFileSize. You will need to open the file by calling CreateFile but that's a cheap operation. Your assumption that opening a file is expensive, even a 12GB file, is false.

You could use the following function to get the job done:

__int64 FileSize(const wchar_t* name) {     HANDLE hFile = CreateFile(name, GENERIC_READ,          FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,          FILE_ATTRIBUTE_NORMAL, NULL);     if (hFile==INVALID_HANDLE_VALUE)         return -1; // error condition, could call GetLastError to find out more      LARGE_INTEGER size;     if (!GetFileSizeEx(hFile, &size))     {         CloseHandle(hFile);         return -1; // error condition, could call GetLastError to find out more     }      CloseHandle(hFile);     return size.QuadPart; } 

There are other API calls that will return you the file size without forcing you to create a file handle, notably GetFileAttributesEx. However, it's perfectly plausible that this function will just open the file behind the scenes.

__int64 FileSize(const wchar_t* name) {     WIN32_FILE_ATTRIBUTE_DATA fad;     if (!GetFileAttributesEx(name, GetFileExInfoStandard, &fad))         return -1; // error condition, could call GetLastError to find out more     LARGE_INTEGER size;     size.HighPart = fad.nFileSizeHigh;     size.LowPart = fad.nFileSizeLow;     return size.QuadPart; } 

If you are compiling with Visual Studio and want to avoid calling Win32 APIs then you can use _wstat64.

Here is a _wstat64 based version of the function:

__int64 FileSize(const wchar_t* name) {     __stat64 buf;     if (_wstat64(name, &buf) != 0)         return -1; // error, could use errno to find out more      return buf.st_size; }  

If performance ever became an issue for you then you should time the various options on all the platforms that you target in order to reach a decision. Don't assume that the APIs that don't require you to call CreateFile will be faster. They might be but you won't know until you have timed it.

like image 171
David Heffernan Avatar answered Oct 06 '22 21:10

David Heffernan