in reply to Deleting all the data after a specific position in a text file.

Is there a better way than using truncate ?

What problem do you have with using truncate?

It is a single call and quite efficient.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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.
  • Comment on Re: Deleting all the data after a specific position in a text file.

Replies are listed 'Best First'.
Re^2: Deleting all the data after a specific position in a text file.
by pankaj_it09 (Scribe) on Nov 18, 2011 at 13:06 UTC
    The problem is that we have to calculate the file size and then subtract to get the value of the truncate LENGTH.
      You does not have to. If you mean size in bytes rather than characters, this can work:
      seek $FH,-100, 1 or die $!; truncate "file", tell $FH or die $!; # Cannot use $FH in truncate, it' +s closed
      The problem is that we have to calculate the file size and then subtract to get the value of the truncate LENGTH.

      Is that soo difficult?

      Maybe you're making the same mistake as the other posters and thinking you need to open the file first and seek around in it? You don't:

      c:\test>dir junk.dat 18/11/2011 19:23 893 junk.dat c:\test>perl -E"$f=pop; $s=-s($f); truncate $f, $s-40 or die $!" junk. +dat c:\test>dir junk.dat 18/11/2011 19:24 853 junk.dat

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      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.