The simple answer is NO!

Think of a disk file like an old fashioned cassette tape. You start at the beginning of the tape and write stuff on the tape. You cannot go into the middle of the tape to delete stuff or add extra stuff - the tape recording has to be contiguous - a disk file is the same way. You can't replace a song in the middle of the tape with another song.

If you want to truncate a disk file (so the stuff at the beginning is not there anymore), the only way to do that is to "make a new cassette tape". Basically you read the file and process the data you want, then copy the rest of the original data to a brand new file. Then you rename that brand new file to the original file name. Whola, you have processed the data and truncated the original file.

In the cassette analog world, you would play cassette 1 until you skipped over the stuff you didn't want to save, and then start recording the output from cassette 1 onto a new cassette 2 from that point. The disk file system essentially works the same way.

THERE MAY BE ANOTHER WAY FOR YOU:

Unlike a cassette tape, a disk file can be "fast forwarded" very, very quickly.
As another analogy, think about a bookmark. When you read a long novel, you put a bookmark in so that you can restart reading again where you left off. You can do the same thing with a disk file. There is function, tell() where you can find out exactly what byte from the beginning of the file that you are currently at. There is a function, seek() where you can go back to that exact byte position where left off before.

So instead of actually truncating the file (which requires copying all the data that you haven't processed to another file), you can keep track of where you are in the input file (the exact byte offset). Going back to that exact byte can happen quickly, much faster than re-reading the first part of the novel. Of course this means that the actual storage space of the file on the disk doesn't get smaller.

You are asking about something that is possible, but unusual. Can you tell us more about what your application is?
Of course to seek() to where you left off, that info would have to be stored somewhere - maybe in another file or DB?


In reply to Re: Fastest way of changing the beginning of a txt file.. by Marshall
in thread Fastest way of changing the beginning of a txt file.. by pedrete

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.