On Fri, 18 Jan 2002 17:54:35 +0000 (GMT), Richard Smith wrote:

>> How about the "seek" function?
>
>I have never used that function before. Just to clarify, if I use
>
>seek FILEHANDLE, 0;
>
>That will reset to the start of the file?

No, but

        seek FILEHANDLE, 0, 0;

will.

Oh, and open the file in "+<" mode, and don't forget to truncate() the
file (handle) before writing anything, if your new data could be shorter
than the old data. Otherwise, you'll keep remains ofthe old data.

Alternatively, truncate *after* you've done writing, bu likely you'll
have to seek() again before doing that for it to work. IIRC, that is
necessary on some OSes.

        open FH, "+<$file"; # file *must* exist;
        @lines = <FH>;  # read all lines
        splice @lines, 1, 1; # remove second line
        seek FH, 0, 0;          # start of file
        print FH @lines;        # write it back;
        seek FH, 0, 1;  # make sure it's in a proper state
        truncate FH, tell FH; # shorten the file
        close FH;
        
Untested, though.

-- 
        Bart.

Reply via email to