gri6507 has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I need to append text to a file, but not to the end of the file; one char before the end. This is a text file, which before appending has a \n that I need to overwrite (but keep all the previous contents). Basically, I need the C equivalent to fseek(handle,-1L,SEEK_END). How can I do this?

Replies are listed 'Best First'.
Re: Advanced appending to file
by hardburn (Abbot) on Jul 13, 2004 at 13:51 UTC

    seek() and you will find your answers.

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re: Advanced appending to file
by chime (Friar) on Jul 13, 2004 at 14:37 UTC
    Seek and tell are what you need for moving output around a file
    # Open existing file open(FILE, "+<filename.txt") || or die "error: $!"; seek(FILE, 0, 2); # seek to the end of the file print FILE "On the end"; # Appended to the end of the file seek (FILE, 0, 0); # seek to the begining of the file print File "At the begining"; my $point = tell(FILE); # tell where you are in the file # Here tell will be 15 becuase you are at the end of "At the begining"
    These are all in the perlfunc docs
Re: Advanced appending to file
by dragonchild (Archbishop) on Jul 13, 2004 at 13:54 UTC
    Personally, I'd look at using Tie::File for this, though hardburn's answer is the standard one.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested