Re: Create a file larger than 3 gb

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Tue, 15 Jul 2008 22:50:56 -0400, "CodeTestDummy"
<sharp_mind.TAKETHISOUT@xxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

All,

I am having an issue create a file larger than 3 GB. I am using CreateFile
and WriteFile. Any ideas how to create one past 3 GB? Here is what I am
doing. Thanks in advance...

BOOL bWrite = TRUE;
HANDLE hFile = CreateFile("c:\\test.dat", GENERIC_WRITE,
FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, 0);

while(bWrite == TRUE)
****
NEVER, EVER compare a boolean to a literal truth value! This statement is nonsensical.
Why don't you write if((dwSize >= (1024 * 1024 * 1024 * 5)) == TRUE)? I consider it a
failure of the educational system that code that tests booleans to booleans to obtain,
guess what, the original boolean (if you're lucky) to be completely tasteless, and also
hazardous.

The correct way to write this is
while(TRUE)
since there is no reason to introduce a gratuitous boolean variable to the loop. But if
you insist, the only other correct way to write this would be
while(bWrite)
*****
{
memset(szData, 0, sizeof(szData));
****
I'm curious: you use all kinds of variable names in this example, but you fail utterly to
show us the declarations!!!! Why? How are we supposed to help you if you hide all the
critical information required to analyze the program?

By the way, the correct way to handle this is to write
::ZeroMemory(szData, sizeof(szData));
and do it OUTSIDE the loop, since there is no reason to zero it a second, third, etc. time
each time around the loop!
*****

WriteFile(hFile, szData, sizeof(szData), &dwBytesWrite, NULL);

Sleep(.5);
****
And what is the point of the above piece of nonsense? It certainly doesn't sleep for 1/2
second; it sleeps for 0ms. I'm surprised it even compiles. Why would you want to Sleep()
anyway?
****

dwSize = GetFileSize(hFile, NULL);
***
If you are using the Hungarian Notation correctly, dwSize is a DWORD. Now, 3GB is
3221225472
or
0xC0000000
which is reasonable, but what are you comparing it to? You are comparing it to

if(dwSize >= (1024 * 1024 * 1024 * 5))
****
5368709120
which is
0x14000000
which if you count carefully CANNOT FIT INTO A 32-BIT DWORD!!!! So dwSize can NEVER be
greater than 5368709120; the maximum dwSize can EVER achieve is 4294967295. The loop will
run forever. Note that since you don't actually check to see if WriteFile actually
*worked*, even when you fill up your disk it will continue to attempt to write data out.

Files in Windows can occupy more than 4.2GB, and therefore, your failure is to have used
GetFileSize inappropriately. It is also generally considered good taste when you are
calling raw APIs to use :: for the scope name so it is obvious you are calling an API.

Therefore, you needed to do one of the two following techniques
LARGE_INTEGER filesize;
filesize.LowPart = ::GetFileSize(hFile, &filesize.HighPart);
OR
LARGE_INTEGER filesize;
::GetFileSizeEx(&filesize);

and then write
if(filesize.QuadPart >= (LONGLONG)1024 * (LONGLONG)1024 * (LONGLONG)1024 *
(LONGLONG)5)
break;
so you are comparing 64-bit values to 64-bit values. You simply threw away the high-order
32 bits of the file size!

Note that if you do a break, there is no need to worry about a boolean variable, and you
could use the while(TRUE) construct.
****
{
bWrite = FALSE;
}
}
CloseHandle(hFile);
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Create a file larger than 3 gb
    ... DWORD dwBytesWrite, dwSize; ... EVER compare a boolean to a literal truth value! ... and do it OUTSIDE the loop, since there is no reason to zero it a second, ... but what are you comparing it to? ...
    (microsoft.public.vc.mfc)
  • Re: Create a file larger than 3 gb
    ... DWORD dwBytesWrite, dwSize; ... EVER compare a boolean to a literal truth value! ... since there is no reason to introduce a gratuitous boolean variable to the loop. ... but what are you comparing it to? ...
    (microsoft.public.vc.mfc)
  • Re: [re-post] why no output
    ... Surely you want to loop while get_script is returning ... But if you're bothering to declare it as returning bool rather than ... you should be comparing its return value with true or false, ... you should never declare a boolean value to true or false. ...
    (comp.lang.c)
  • Re: MKDir not working
    ... Public Sub MkDirs ... first argument of the Split function in the active loop statement; ... track and return a Boolean status from the function. ... MkDirs = MkDirs + Err.Number ...
    (microsoft.public.vb.general.discussion)
  • Re: GNAT compiler switches and optimization
    ... allocating and freeing arrays, the the effects ... But I imagined allocation is just what is happening all the time ... type LIST is array of BOOLEAN; ... 2000 loop ...
    (comp.lang.ada)