LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Breaking up a file into blocks (https://www.linuxquestions.org/questions/programming-9/breaking-up-a-file-into-blocks-181131/)

otnaicus 05-13-2004 01:22 PM

Breaking up a file into blocks
 
Hi,

Would like to ask if anyone knows how to split up a file into blocks of a certain size. I'm programming in C. Any help would be very much appreciated.

Thanks.

TheOther1 05-13-2004 03:36 PM

You want to write something to do this? How about reading the file into an array of desired size, stop reading, write array out to file.001, read next portion, write out to file.002, etc...

jim mcnamara 05-13-2004 04:49 PM

FWIW -

dd will do that, and you don't have to write code to do it.
split will also whack files into smaller pieces.

otnaicus 05-14-2004 12:07 AM

Thanks for your replies... Actually, what I'm trying to do is to write a program where cryptography is involved. As such, I would like to be able to split the file up into blocks within the program itself. I'll give the array method a try...

Thanks again... :)

Hko 05-14-2004 11:31 AM

If you are going to make it a GPL'd program, then use the sources for the GNU tool "split" (part of the GNU coreutils package)

marsques 05-18-2004 01:29 PM

yeah could you give me an example on how "dd" would do it...

lets say i got file 20MB and i want 5MB chunks...

TheOther1 05-18-2004 08:10 PM

if you have split, you can do this:

Code:

split -b 5242880 /path/to/file1 /path/to/chunks/outputprefix
so:
Code:

split -b 5242880 /home/user/CoolMovie.mpg /tmp/split/movie
would split 20 MB movie.mpg into 5 M chunks and name them movieaa, movieab, movieac and moviead in the /tmp/split dir

TheOther1 05-18-2004 08:26 PM

With dd:

dd if=your-input-file of=first-out-file skip=0 count=10240
dd if=your-input-file of=second-out-file skip=10240 count=10240
dd if=your-input-file of=third-out-file skip=20480 count=10240
dd if=your-input-file of=fourth-out-file skip=30720 count=10240

etc... Assuming block size is 512 bytes.

marsques 05-19-2004 01:50 PM

Quote:

Originally posted by TheOther1
if you have split, you can do this:

Code:

split -b 5242880 /path/to/file1 /path/to/chunks/outputprefix
so:
Code:

split -b 5242880 /home/user/CoolMovie.mpg /tmp/split/movie
would split 20 MB movie.mpg into 5 M chunks and name them movieaa, movieab, movieac and moviead in the /tmp/split dir

thanks... but what is 5242880? sorry i'm a bit confused...

using the 20MB example... hehe... i get it... 5242880 is 5MB right? so the size of the chunk should be specified in bytes?

TheOther1 05-19-2004 11:13 PM

Right! :)


All times are GMT -5. The time now is 11:10 AM.