Click here to Skip to main content
15,072,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

i need to write a file from CByteArray, normally i do fast with:
WriteFile(hFile, btMemArray->GetData(), dwBytesToWrite, &dwBytesWritten, NULL);

but now i need a progressbar to follow the extraction, so i need to read chunks from cbytearray and write to file, but i cannot find a solution can you help me?

Thanks :)

What I have tried:

int nOffset = 0;

do
{
   bDone = WriteFile(hFile, btMemArray->GetData() + nOffset, 1, &dwBytesWritten, NULL);

   nOffset++;

} while (nOffset < btMemArray->GetSize());

<pre>

The my best was this at moment but i will need obviously bigger chunk than 1 help me to do this last step
Posted
Updated 2-May-20 5:06am
v2
Comments
Richard MacCutchan 2-May-20 8:41am
   
Why are you calling ZeroMemory when CopyMemory immediately overwrites the zeros?

It is not clear how you are co-ordinating between the reading (from where?) and the writing of the data, or how much actual data you will process.
[no name] 2-May-20 9:02am
   
There is no need of another buffer. CByteArray does give you the start address with GetData and the length of the array with GetSize so you can easely write chunk by chunk directly.
Drakesal 2-May-20 10:51am
   
int nOffset = 0;

do
{
bDone = WriteFile(hFile, btMemArray->GetData() + nOffset, 1, &dwBytesWritten, NULL);

nOffset++;

} while (nOffset < btMemArray->GetSize());

I have done this progress but how to use with higher chunk?
[no name] 2-May-20 11:16am
   
See Richard's answer which looks very good.
Drakesal 2-May-20 12:05pm
   
Thanks very much Richard you solved totally my problem, god bless you ;)

1 solution

It is only necessary to call GetSize and GetData once, before you start the loop. You can then use the returned values to walk the buffer writing your fixed size chunks. Something like:
C++
int chunksize = 1024; // or whatever size you want
DWORD dwBytesWritten;

int arraySize = btMemArray->GetSize();
byte* pByte = btMemArray->GetData();

while (arraySize > 0)
{
    if (arraySize < chunksize)
    {
        chunksize = arraySize; // any final short block
    }
    WriteFile(hFile, pByte, chunksize, &dwBytesWritten, NULL);
    // check for errors here
    pByte += chunksize;
    arraySize -= chunksize; // reduce the amount left write
}
   
v6
Comments
[no name] 2-May-20 11:14am
   
Looks fine, +5
Richard MacCutchan 2-May-20 11:17am
   
I had to rewrite it a few times as my brain got confused. :(
[no name] 2-May-20 11:19am
   
I had a solution 5 Min after you, but your code is much more readable than mine.
A small thing, here is a semicolon too much btMemArray->;GetData();
Richard MacCutchan 2-May-20 11:31am
   
Thanks, corrected. My old brain and the editor doing battle - you can see who won.
[no name] 2-May-20 11:36am
   
:-) same here a lot of time. Btw. another thing I see now: In case chunksize is bigger than the arraySize in the beginning one should limit it also at beginning of the loop.
Richard MacCutchan 2-May-20 11:46am
   
You are right of course. Is that better?
[no name] 2-May-20 11:50am
   
Perfect!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900