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.
Thanks, Jose. I've seen that, but how do I get the HANDLE value that needs to be passed? I can initialize a FILE type variable without any problem, but how do I get the HANDLE? Thank you for your patience. Most of my experience is in writing embedded firmware; I haven't touched this Windows stuff in years :\ ...
The _get_osfhandle() function exists in Win32 for this very purpose: convert a standard C handle to a Win32 handle, so you can do cool Win32 stuff with it, like GetFileSize().
It is true that you shouldn't use both the HANDLE and the FILE* at the same time...
Is there not an easier way to get the filesize if he just wants to stay in the stream functions? I don't really know, as I program almost entirely in C/Win32 API, but I seem to recall in going through my C/C++ books that there is the capability to get the file size without having to resort to the Win32 API.
Further to Duoas' recommendation, to use "seek" and "tell", the reference has something very good:
1 2 3 4
/* Get size of file: */
fseek(f, 0, SEEK_END);
fSize = ftell(f);
rewind(f);
Note that that's my rewrite of the code and that fSize is a long integer. I think that's what he meant. I'm not sure if there's some C++ way of doing it, but that would be the C way...