It would be ugly. You would have to copy the file from the end of your current line, update the current line, then put the copied data back, after your new file position. I actually started to write some example code, but it is such a terrible idea I quickly gave up.

You would probably load the entire file into an array, line per record, then go through the array, keeping a note of how many bytes into the file you are. When you update a record, seek to the end of the old record, before its \n, write the additional info plus new \n, then overwrite the rest of the file with the rest of the records in your array. If you really want to keep the same file, it would be better to build the entire new record set in memory, then blat it all out in one go, overwriting the old file. Power loss, or a crash, will probably cause data loss.

If you really want to go mad, you could process one record at a time, then when you came to write out, read a few records from your file, write your data, then do a sort of read/write shuffle through the file to move everything up a few bytes. For this approach to work, you would have to be smoking something pretty special ;-)

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to Re^4: Write in files by Random_Walk
in thread Write in files by lolz

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.