Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to erase the file content

by kamesh3183 (Beadle)
on Sep 08, 2005 at 06:36 UTC ( [id://490113]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks
How to erase the content of a file without closing and opening it again
Update: I am using Win2k and ActiveState Perl 5.8.6.

Thanks
Kamesh

Replies are listed 'Best First'.
Re: How to erase the file content
by jonadab (Parson) on Sep 08, 2005 at 09:57 UTC
    How to erase the content of a file without closing and opening it again

    As far as I am aware, every way to erase the contents of a file on NT involves, either directly or indirectly, opening it in overwrite mode, writing nothing, and closing. If you already had the file open, that will mean closing and opening again.

    update: Unless you want to just delete the file. That will also have the side-effect of erasing its contents. If that's what you want, Perl's unlink command will do it for you.

Re: How to erase the file content
by fishbot_v2 (Chaplain) on Sep 08, 2005 at 13:15 UTC

    From the question, it really sounds to me that the file is already open, and that the OP doesn't want to close it and re-open it. In which case, they are looking for truncate.

    open my $fh, ">", "./foo.txt" or die( "Couldn't etc." ); print $fh "$_\n" for 1..100; truncate $fh, 0; print $fh "$_\n" for 1..10; close $fh;

    Now foo.txt contains 1..10 only.

    Update: As parv mentioned up-thread, this might close and re-open under the hood. It's not clear if you don't want to re-open for programatic reasons or for performance reasons.

Re: How to erase the file content
by parv (Parson) on Sep 08, 2005 at 06:44 UTC

    Assuming an Unix operating system...

    I was going to say truncate but first i tried a kernel trace which indeed opens and closes the file. That leads me to say that redirecting stdin or /dev/null would do the same.

    Only way seems to be rm -f file && touch file. Then again (seen via another kernel trace), touch also opened and closed the file.

    Well, i am out of ideas...

      OP has mentioned Win2K. Then why you are assuming unix.

        The OP wanted to truncate a file without opening and closing the file.

        I guess the OP updated the node and deleted the original contents. OP did not mention the OS before parv made the post

Re: How to erase the file content
by sh1tn (Priest) on Sep 08, 2005 at 07:06 UTC
    Just open in '>' mode:
    { open my $fh, "> $file" or die "cannot open $file: $!" }


Re: How to erase the file content
by newroz (Monk) on Sep 08, 2005 at 07:14 UTC

    perl -pi -e 's/.*//s' file
Re: How to erase the file content
by gopalr (Priest) on Sep 08, 2005 at 07:57 UTC

    Hi Kamesh,

    Take a look at File::Slurp Module.

    Thanks,
    Gopal.R

Re: How to erase the file content
by knsridhar (Scribe) on Sep 08, 2005 at 08:53 UTC
    
    try this
    
    system(": > $filename");
    
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://490113]
Approved by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-23 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found