LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2004, 01:22 PM   #1
otnaicus
LQ Newbie
 
Registered: May 2004
Posts: 2

Rep: Reputation: 0
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.
 
Old 05-13-2004, 03:36 PM   #2
TheOther1
Member
 
Registered: Feb 2003
Location: Atlanta, GA
Distribution: RHAS 2.1, RHEL3, RHEL4, SLES 8.3, SLES 9, SLES9_64, SuSE 9.3 Pro, Ubuntu, Gentoo
Posts: 335

Rep: Reputation: 32
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...
 
Old 05-13-2004, 04:49 PM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
FWIW -

dd will do that, and you don't have to write code to do it.
split will also whack files into smaller pieces.
 
Old 05-14-2004, 12:07 AM   #4
otnaicus
LQ Newbie
 
Registered: May 2004
Posts: 2

Original Poster
Rep: Reputation: 0
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...
 
Old 05-14-2004, 11:31 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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)
 
Old 05-18-2004, 01:29 PM   #6
marsques
Member
 
Registered: Jan 2004
Location: Manchester
Distribution: slackware...
Posts: 344

Rep: Reputation: 32
yeah could you give me an example on how "dd" would do it...

lets say i got file 20MB and i want 5MB chunks...
 
Old 05-18-2004, 08:10 PM   #7
TheOther1
Member
 
Registered: Feb 2003
Location: Atlanta, GA
Distribution: RHAS 2.1, RHEL3, RHEL4, SLES 8.3, SLES 9, SLES9_64, SuSE 9.3 Pro, Ubuntu, Gentoo
Posts: 335

Rep: Reputation: 32
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
 
Old 05-18-2004, 08:26 PM   #8
TheOther1
Member
 
Registered: Feb 2003
Location: Atlanta, GA
Distribution: RHAS 2.1, RHEL3, RHEL4, SLES 8.3, SLES 9, SLES9_64, SuSE 9.3 Pro, Ubuntu, Gentoo
Posts: 335

Rep: Reputation: 32
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.
 
Old 05-19-2004, 01:50 PM   #9
marsques
Member
 
Registered: Jan 2004
Location: Manchester
Distribution: slackware...
Posts: 344

Rep: Reputation: 32
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?
 
Old 05-19-2004, 11:13 PM   #10
TheOther1
Member
 
Registered: Feb 2003
Location: Atlanta, GA
Distribution: RHAS 2.1, RHEL3, RHEL4, SLES 8.3, SLES 9, SLES9_64, SuSE 9.3 Pro, Ubuntu, Gentoo
Posts: 335

Rep: Reputation: 32
Right!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Breaking out of Chroot Aeiri Linux - Security 1 02-26-2005 02:10 PM
allocate file blocks via sys call jwstric2 Programming 1 10-13-2004 02:22 PM
breaking waves... marsques Linux - Software 4 05-18-2004 01:25 AM
Script file to replace large text blocks in files? stodge Linux - Software 0 09-27-2003 10:53 AM
Breaking Windows cli_man General 17 04-20-2002 02:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration