Copying 1024 bytes data in 3-bytes chunk | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Copying 1024 bytes data in 3-bytes chunk

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-12-2011
royalibrahim royalibrahim is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 21 January 2014, 5:25 AM EST
Posts: 351
Thanks: 56
Thanked 5 Times in 5 Posts
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.


Code:
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.
Sponsored Links
    #2  
Old 04-12-2011
disaster disaster is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 28 April 2011, 9:14 AM EDT
Posts: 45
Thanks: 7
Thanked 5 Times in 5 Posts
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
The Following User Says Thank You to disaster For This Useful Post:
royalibrahim (04-12-2011)
Sponsored Links
    #3  
Old 04-12-2011
royalibrahim royalibrahim is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 21 January 2014, 5:25 AM EST
Posts: 351
Thanks: 56
Thanked 5 Times in 5 Posts
Quote:
Originally Posted by disaster View Post
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
Yes, you are absolutely right!! Thanks a ton for the hint
    #4  
Old 04-13-2011
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 23 October 2014, 9:54 PM EDT
Location: NM
Posts: 10,250
Thanks: 282
Thanked 803 Times in 747 Posts
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Error PHP Fatal error: Allowed memory size of 67108864 bytes exhausted(tried to allocate 401 bytes) elamurugu Shell Programming and Scripting 1 12-13-2010 02:33 PM
Printing bytes ahmedwaseem2000 Shell Programming and Scripting 12 05-26-2010 09:31 AM
Command to ping 1500 bytes of data to a destination system rvan IP Networking 2 11-16-2009 05:08 AM
Remove first N bytes and last N bytes from a binary file on AIX. naveendronavall Shell Programming and Scripting 1 05-24-2008 11:06 AM
awk: seeking to bytes karyn1617 Shell Programming and Scripting 8 02-07-2005 10:51 PM



All times are GMT -4. The time now is 05:31 AM.