First, if you open a file, it will be opened by default in text format. That means that \n (new lines), etc mean things.

If the file you are going to copy is just a bunch of bits, then you should do something like this to just completely ignore any such bytes:

$out = "output_path"; open (OUTBIN, '>',"$out") || die "unable to open $out"; binmode(OUTBIN) || die "unable to set binmode $out";
The above says that I'm opening the $out file and I'm just gonna send bits to it! This next part does the copy..
open(INBIN, "<$x") || die "unable to open $x"; binmode(INBIN) || die "unable to set binmode $x"; while (read(INBIN, my $buff, 8 * 2**10)) { print OUTBIN $buff; } close(INBIN) || die "unable to close inbin"; close(OUTBIN) || die "unable to close outbin";
The above says: open filepath $x, then set it for binary read. Each read will be 8*2**10 or 8 * 1024= 8192 bytes. Perl will help out here as it keeps track of the number of bytes in $buff. If the $buff has less than what you expect for the maximum read, this is no problem!

2**10=1024 is a "magic number" for the hardware.
A typical Unix system will read from the hard drive in increments of 4x that or 4096 bytes. Here the buffer size is twice that or 8192 bytes. It is counter-intuitive, but increasing the buffer size can actually slow things down if you have a smart disk system.

The important part is to set BINMODE. And for the read, you will have to specify a size that should be in increments of 1024 (a magic number).


In reply to Re: bin mode file copy by Marshall
in thread bin mode file copy by smanicka

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.