-
November 11th, 2010, 02:04 AM
#1
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.
-
November 11th, 2010, 02:13 AM
#2
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
-
November 11th, 2010, 02:39 AM
#3
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;
...
}
-
November 11th, 2010, 03:02 AM
#4
Re: Getting filesize using GetFileSizeEx
Originally Posted by reachb4
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);
-
November 12th, 2010, 02:59 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|