Cleaning out a text file.

Discussion in 'Perl Misc' started by Avatar, Jul 15, 2003.

  1. Avatar

    Avatar Guest

    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;
     
    Avatar, Jul 15, 2003
    #1
    1. Advertising

  2. Avatar

    Peter Wilson Guest

    "Avatar" <> wrote in message
    news:...
    > 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
     
    Peter Wilson, Jul 15, 2003
    #2
    1. Advertising

  3. Avatar

    Uri Guttman Guest

    >>>>> "TC" == Tony Curtis <> writes:

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


    >> Why not just unlink it?


    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jul 15, 2003
    #3
  4. Uri Guttman wrote:
    >>>>>> "TC" == Tony Curtis <> writes:

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

    >
    > >> Why not just unlink it?

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

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

    >
    > 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
     
    Jürgen Exner, Jul 15, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Magnus Henriksson

    Cleaning out my disk

    Magnus Henriksson, Oct 20, 2006, in forum: XML
    Replies:
    3
    Views:
    498
    roy axenov
    Oct 20, 2006
  2. DR
    Replies:
    0
    Views:
    659
  3. Replies:
    1
    Views:
    275
    Richard Bos
    Apr 10, 2008
  4. Larry Hastings
    Replies:
    0
    Views:
    315
    Larry Hastings
    Apr 23, 2009
  5. DR
    Replies:
    0
    Views:
    760
Loading...

Share This Page