Re: Create a file larger than 3 gb
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Wed, 16 Jul 2008 02:00:14 -0400
On Wed, 16 Jul 2008 01:04:27 -0400, "CodeTestDummy"
<sharp_mind.TAKETHISOUT@xxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Thanks for the help. You made some good points. Here us my original code.****
I was planning to add the error checking after I got it to work. Thanks
again for the advise. Please feel free make any more comments.
DWORD dwBytesWrite, dwSize;
BOOL bWrite = TRUE;
TCHAR szData[1024];
HANDLE hFile = CreateFile("c:\\test.tmp", GENERIC_WRITE,
FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, 0);
That should be _T("c:\test.tmp") and note that it is not always possible to write in the
root directory of a system, so this is a poor choice of destination.
*****
****
while(bWrite == TRUE)
{
memset(szData, 0, sizeof(szData));
There is no reason to zero this more than once
*****
WriteFile(hFile, szData, sizeof(szData), &dwBytesWrite, NULL);****
// Without this, the CPU gets hit hard.
Sleep(.5);
So what? It's *supposed* to get "hit hard" if you are in an infinite loop, and in any
case, 0.5 is a meaningless value (the correct value is the delay time in milliseconds; so
I have no idea what possible meaning a floating-point value could have here)
***
Joseph M. Newcomer [MVP]
dwSize = GetFileSize(hFile, NULL);
if(dwSize >= (1024 * 1024 * 1024 * 5))
{
bWrite = FALSE;
}
}
CloseHandle(hFile);
"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:duqq74151usqfdp7qcecltmbcrc6ff59m7@xxxxxxxxxx
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.
****
{Joseph M. Newcomer [MVP]
bWrite = FALSE;
}
}
CloseHandle(hFile);
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: Create a file larger than 3 gb
- From: Pete Delgado
- Re: Create a file larger than 3 gb
- References:
- Re: Create a file larger than 3 gb
- From: Joseph M . Newcomer
- Re: Create a file larger than 3 gb
- From: CodeTestDummy
- Re: Create a file larger than 3 gb
- Prev by Date: Re: Document corrupted after showing dialog
- Next by Date: Re: How can i get the stored application descriptions and path in windows system
- Previous by thread: Re: Create a file larger than 3 gb
- Next by thread: Re: Create a file larger than 3 gb
- Index(es):
Relevant Pages
|