I have a binary file with "records" of variable lengths. Each record ends with a '\n'.

This makes no sense. Let's say that your records consist of a number of unsigned longs, it doesn't matter how many. When these are packed to their 4-byte binary representations, there are ~67,000,000 legitimate values where one or more bytes of the four will be "\n". These include the numbers 10, 266, 522, 778, 1034, 1290, 1546, 1802, 2058, 2314, 2560, 2561, 2562, 2563, 2564, 2565, 2566, 2567, 2568 ...

When you try to read a record containing one of these values back expecting a "\n" as the delimiter, the IO code will encounter the "\n" embedded within one of your binary values and truncate the record.

And you will have the same problem with all types of packed binary numbers. Shorts & longs, signed & unsigned, and floating point also.

So, if you are writing raw binary values to a file and trying to use "\n" as a delimiter--or any other character--you will not be able to read that data back successfully. Period.

If you are not writing raw binary values to the file, then you are goingt o have to clarify what you mean by "binary data", because your question as asked simply doesn't make any sense.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Updating Binary Files With variable record lengths by BrowserUk
in thread Updating Binary Files With variable record lengths by rorokimdim

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.