Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

empty a file

by GS (Acolyte)
on Jun 27, 2004 at 15:58 UTC ( [id://369976]=perlquestion: print w/replies, xml ) Need Help??

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

Dears monks,
how do I empty the contents of a text file in perl,
I have to delete evrything from a file before updating it.
Thanks in advance. Forgot to mention that I am oprning this file in append mode
open HANDLE,">>","/path/to/file" or die "Cannot overwrite file: $!";
Initially at open I need to delete all contents.

Replies are listed 'Best First'.
Re: empty a file
by Nkuvu (Priest) on Jun 27, 2004 at 16:08 UTC
    Short answer: open FILEHANDLE, ">filename.txt" or die "Hork! $!\n";

    Slightly longer answer: Read perldoc perlopentut and then read perldoc -f open

Re: empty a file
by Joost (Canon) on Jun 27, 2004 at 16:09 UTC
      Forgot to mention that I am opeing this file in append mode.
      So I can not open it in write mode.

        perldoc -f truncate

        Ciao, Valerio

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: empty a file
by l3nz (Friar) on Jun 27, 2004 at 20:03 UTC
    Either you open the file in append mode or you truncate the file. The two thing don't logically go together.

    Why don't you simply open the file in non-append mode? you'd get the same result as truncate plus append.

Re: empty a file
by Scarborough (Hermit) on Jun 28, 2004 at 12:02 UTC
    If you are openning the file using the data and then writting new data to the file the simple way appears to me as follows
    open IN, $file; #do what you need to do close IN; open OUT ">$file"; #print out the data close OUT;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found