C++ Get File Size
-
Hello. I'm writing a program using M$ Visual C++. Is there a simple API for getting the file size? Something like GetFileSize? :) I'm trying to read out bytes from a binary file, and I think it's prematurely reading out an EOF character. So, I need to query the filesize from the OS. Thanks.
-
Hello. I'm writing a program using M$ Visual C++. Is there a simple API for getting the file size? Something like GetFileSize? :) I'm trying to read out bytes from a binary file, and I think it's prematurely reading out an EOF character. So, I need to query the filesize from the OS. Thanks.
pcpro178 wrote:
Is there a simple API for getting the file size? Something like GetFileSize?
You mean something like GetFileSize()[^]? Yeah, there is.
It is a crappy thing, but it's life -^ Carlo Pallini
-
pcpro178 wrote:
Is there a simple API for getting the file size? Something like GetFileSize?
You mean something like GetFileSize()[^]? Yeah, there is.
It is a crappy thing, but it's life -^ Carlo Pallini
-
Well, yes, but GetFileSize requires a variable of type HANDLE to be passed to it. I have a FILE variable instantiated, but how do I go from one to the other?
If you insist on the FILE pointer, you can do this:
fseek(file, 0, SEEK_END);
filesize = ftell(file);
fseek(file, 0, SEEK_SET);> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <
-
Well, yes, but GetFileSize requires a variable of type HANDLE to be passed to it. I have a FILE variable instantiated, but how do I go from one to the other?
There are multiple ways including the following simplified code (will not compile):
FILE* pFile = fopen();
if (pFile)
{
int fileNum = _fileno(pFile)
HANDLE hFile = (HANDLE) _get_osfhandle(fileNum);
GetFileSize(hFile);
} -
There are multiple ways including the following simplified code (will not compile):
FILE* pFile = fopen();
if (pFile)
{
int fileNum = _fileno(pFile)
HANDLE hFile = (HANDLE) _get_osfhandle(fileNum);
GetFileSize(hFile);
}