Discussion:
Is any Win32 API to set size of disk file directly?
(too old to reply)
Frank
2004-01-30 04:44:49 UTC
Permalink
For some reason, I need to set size of file randomly directly. But after
searched msdn, I have not found any api or information about that. So, would
any one tell me Is any Win32 API to set size of disk file directly or, not,
how to do that with other methods.

Thanx :-)

--
Frank F.Han

+-----------------------------------------+
| winsays@:-)hotmail:-).com |
+-----------------------------------------+
Martin Horst
2004-01-30 07:15:13 UTC
Permalink
Hi,
Post by Frank
For some reason, I need to set size of file randomly directly. But after
searched msdn, I have not found any api or information about that. So, would
any one tell me Is any Win32 API to set size of disk file directly or, not,
how to do that with other methods.
Thanx :-)
maybe this function will help you.

BOOL SetFileLength(HANDLE hFile, INT64 i64FileLength)
{
DWORD dwResult;
LONG lLow, lHigh;
BOOL bFailed;

SetLastError(0);
if ( i64FileLength < 0 )
return FALSE;

ASSERT(hFile != NULL);
ASSERT(hFile != INVALID_HANDLE_VALUE);
if ( hFile == NULL || hFile == INVALID_HANDLE_VALUE )
return FALSE;

lLow = LONG(i64FileLength & 0xFFFFFFFF);
lHigh = LONG((i64FileLength >> 32) & 0xFFFFFFFF);
dwResult = SetFilePointer(hFile, lLow, &lHigh, FILE_BEGIN);
bFailed = (dwResult == INVALID_SET_FILE_POINTER && GetLastError() != 0);

if ( !bFailed )
bFailed = !::SetEndOfFile(hFile);

ASSERT(!bFailed);
return !bFailed;
}


Best regards
Martin Horst

Loading...