Cleaning out a text file.

A

Avatar

I am trying to just open a file and clear it. Is there an easier way
to do this than the way I have done.

open (DELETE, ">$filename");
print DELETE "";
close DELETE;
 
P

Peter Wilson

Avatar said:
I am trying to just open a file and clear it. Is there an easier way
to do this than the way I have done.

open (DELETE, ">$filename");
print DELETE "";
close DELETE;

Why not just unlink it?

Peter
 
U

Uri Guttman

TC> Well, it's possible that the file needs to remain in
TC> existence, but be empty.

TC> The OP should just open for write and close immediately
TC> (and check the success of the open() too).

perldoc -f truncate

or unlink and utime will do it.

uri
 
J

Jürgen Exner

Uri said:
perldoc -f truncate

or unlink and utime will do it.

Careful! If you unlink the file then only this directoy entry will be
removed. If there are several hard links pointing to the same file, then
maybe the OPs intention was to actually empty this original file.
Unlink will actually remove the entry and when re-creating a file with the
same name it will be a new file that is not related to the old file any
longer, i.e. the other hardlinks will still point to the old file.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top