If you are using MS-Windows, you probably need to do "binmode" on both the input and output files after you open them, to turn off "crlf" mode. If you don't do that, the binary data will get corrupted.

Apart from that, you really should set the input recorder separator variable to "AZII*" (as recommended in replies to your previous thread), because using line-oriented i/o on non-text data is just strange. Try something like this:

use strict; open( FILE, '1.dat') or die "1.dat: $!"; binmode FILE; $/ = "AZII*"; my $num = 0; while (<FILE>) { chomp; next unless length(); # skip the initial (empty) input record $num++; open (DES, ">out/$num.txt") or die ( "out/$num.tiff" ); binmode DES; print DES "AZII*".$_; close DES; last if ( $num == 5 ); }
(Updated to add the "next unless length()" condition -- if the file begins with "AZII*", the first input record will be empty after the "chomp".)

You may need to play with that, e.g. if "AZII*" is supposed to be followed by a line-feed (or carriage-return + line-feed).

I don't understand why the output file names in the OP were set to "$num.txt" -- these are not text files. You say they are image files (tiff), so why not use the appropriate extension for the output files? Am I missing something in your description of the problem?


In reply to Re: File Extraction - Cont... by graff
in thread File Extraction - Cont... by karavay

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.