Re: get file size??
"Steve" <steve@sv.sv> ha scritto nel messaggio
news:eHYkL34yIHA.1772@TK2MSFTNGP03.phx.gbl...
I am trying to retrieve file size in VC++
I read this article
http://www.codeproject.com/KB/files/filesize.aspx
but I need to write the code which works/compiles for both ASNI string as
well as UNICODE string. Any suggestions??
the API GetFileSize requires handle to the file. How do I simply get
handle to a file?
Norbert already posted a valid solution.
However, if you want to open a file using Win32 APIs and get a handle, and
then use GetFileSize(Ex) to get file size, you may use code like this:
<code>
// CString filename ...
//
// Open file for reading
//
HANDLE hfile = ::CreateFile( filename, GENERIC_READ,
FILE_SHARE_READ, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0 );
if ( hFile == INVALID_HANDLE_VALUE )
... error
//
// Get file size
//
LARGE_INTEGER fileSize;
if ( ! ::GetFileSizeEx( hFile, &fileSize ) )
{
// Error in file size
// Close file before quitting
::CloseHandle( hFile );
...
}
//
// Close file
//
::CloseHandle( hFile );
// Avoid dangling references
hFile = INVALID_HANDLE_VALUE;
</code>
Note that I prefer using GetFileSizeEx instead of GetFileSize. In fact,
GetFileSizeEx has a better interface, IMHO: for example, it is more clear
how to check for errors, and it returns the file size in a single 64-bit
integer, instead with GetFileSize the 64-bit integer is split in two
separate 32-bit DWORDs, one returned as return value, and the other passed
as output parameter...
GetFileSizeEx
http://msdn.microsoft.com/en-us/library/aa364957.aspx
HTH,
Giovanni