Just to elaborate a bit more on what tachyon said: When you open a file for write you use ">file". This will destroy the contents of "file" if it already existed and you will be able to write to the file. ">" still works the same way even if you add read support, and I'm pretty sure the perl documentation warns about what you ended up doing ("+>"): opening the file for read and write, but destroying the contents before you got a chance to read.

What you want is "+<". You can now read and write to the file, but you will want to familiarize yourself with the seek command, which will allow you to write to different spots in the file.

Also keep in mind that the file does not truncate itself. For instance if you have a file that contains "oooo", you seek to the beginning of the file and write "XXX", your file will contain "XXXo".

In reply to Re: read and write to the same file by archen
in thread read and write to the same file by dee00zee

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.