Copying 1024 bytes data in 3-bytes chunk

Hi,

If I want to copy a 1024 byte data stream in to the target location in 3-bytes chunk, I guess I can use the following script.

dd bs=1024 count=3 if=/src of=/dest

But, I would like to know, how to do it via a C program. I have tried this with memcpy(), that did not help.

The dd function you posted would actually copy three times a junk of 1024 bytes, ie. 3K bytes in total.
But I understand your question in such a way that you want to read 3 bytes in at once and do that same 300 times, right?

memcpy() is not the right function when you want to store it into a file. You simply need to store 3 bytes in a buffer of char buf[3]; and then use fwrite to write this buffer into a file. Wrap a loop around it so it gets done until 1024bytes are written and you're done

1 Like

Yes, you are absolutely right!! Thanks a ton for the hint :slight_smile:

1024/3 = 341.333333.....

This means you cannot copy 1024 bytes only in chunks of three and come out with an even 1024 bytes, you get 1023 or 1026 bytes copied.