There are various ways to do this:

Just pick one method and stick with it: loop over the big file to read "x" bytes, open a suitable output, write the chunk, and close it. Don't repeat any data across chunks -- that will mess you up later.

Since you're talking about "sending" the chunks, I assume they're relatively small, so one chunk fits in memory. (If the chunks are too big, you'd need a nested loop to read "sub-chunks" that do fit, outputting each one to the "main chunk", then closing that when it's the right size, and moving on to the next main chunk.)

If the chunks are being stored as separate files, putting them back together again is really simple. Either a single shell command:

cat fatset.chunk* > fatset
or in perl:
{ local $/; # sets input_record_separator to "undef" open(O,">fatset"); for my $f (sort <fatset.chunk*>) { open(I,$f); $_ = <I>; # reads entire chunk at once. print O; } close O; }
Just be sure that if you need to make more than 9 chunks, use leading zeros in the file names for chunks 1-9, so that the names will sort naturally into their proper order. Use  sprintf( 'fatset.chunk%03d', $chunknumber++ ) for that.

Naturally, if you use the perl version, add some error checking...


In reply to Re: Splitting a binary file by graff
in thread Splitting a binary file by warthurton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.