Erasing text from a file

Discussion in 'Ruby' started by Lucas L., May 11, 2008.

  1. Lucas L.

    Lucas L. Guest

    To me it seems it should be a rather obvious thing, but no matter where
    I look, I can't find how to erase text from a file or clear a file. The
    only things I have found are moving the the cursor to the start and
    writing more text over it (but that doesn't get rid of it), or reopening
    the file.

    Is there a way to simple erase text from the file?

    Thanks,
    Lucas
    --
    Posted via http://www.ruby-forum.com/.
     
    Lucas L., May 11, 2008
    #1
    1. Advertising

  2. Lucas L.

    Andrea Fazzi Guest

    Lucas L. ha scritto:
    > To me it seems it should be a rather obvious thing, but no matter where
    > I look, I can't find how to erase text from a file or clear a file. The
    > only things I have found are moving the the cursor to the start and
    > writing more text over it (but that doesn't get rid of it), or reopening
    > the file.
    >
    > Is there a way to simple erase text from the file?
    >
    > Thanks,
    > Lucas
    >


    File.open('file.txt', 'w') { |file| file = nil }
     
    Andrea Fazzi, May 11, 2008
    #2
    1. Advertising

  3. Lucas L.

    Lucas L. Guest

    Andrea Fazzi wrote:
    > File.open('file.txt', 'w') { |file| file = nil }


    That reopens the file. Is there a way to do it without reopening?
    --
    Posted via http://www.ruby-forum.com/.
     
    Lucas L., May 11, 2008
    #3
  4. Lucas L. wrote:
    > Andrea Fazzi wrote:
    > > File.open('file.txt', 'w') { |file| file = nil }


    The "file = nil" part is completely unneccessary. You can just do
    File.open('file.txt', 'w') {}


    > Is there a way to do it without reopening?


    I don't think there is.

    HTH,
    Sebastian
    --
    NP: Depeche Mode - Freestate
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, May 11, 2008
    #4
  5. Lucas L.

    Andrea Fazzi Guest

    Sebastian Hungerecker ha scritto:
    > Lucas L. wrote:
    >
    >> Andrea Fazzi wrote:
    >>
    >>> File.open('file.txt', 'w') { |file| file = nil }
    >>>

    >
    > The "file = nil" part is completely unneccessary. You can just do
    > File.open('file.txt', 'w') {}
    >
    >


    Ok, thank you :)

    Andrea
     
    Andrea Fazzi, May 11, 2008
    #5
  6. Lucas L.

    ts Guest

    Lucas L. wrote:
    > Is there a way to simple erase text from the file?


    perhaps File#truncate

    ---------------------------------------------------------- File#truncate
    file.truncate(integer) => 0
    ------------------------------------------------------------------------
    Truncates _file_ to at most _integer_ bytes. The file must be
    opened for writing. Not available on all platforms.

    f = File.new("out", "w")
    f.syswrite("1234567890") #=> 10
    f.truncate(5) #=> 0
    f.close() #=> nil
    File.size("out") #=> 5



    Guy Decoux
     
    ts, May 11, 2008
    #6
  7. Lucas L.

    Lucas L. Guest

    ts wrote:
    > perhaps File#truncate


    The documentation says this is platform specific, which I'd like to
    avoid.
    File.open will have to do.
    --
    Posted via http://www.ruby-forum.com/.
     
    Lucas L., May 12, 2008
    #7
    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. Alex D.
    Replies:
    4
    Views:
    1,312
    Alejandro Penate-Diaz
    Nov 23, 2005
  2. darrel
    Replies:
    4
    Views:
    571
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    May 5, 2006
  3. Owen Brydon

    Erasing map elements by iterator

    Owen Brydon, Oct 15, 2003, in forum: C++
    Replies:
    5
    Views:
    6,633
    Andrew Koenig
    Oct 16, 2003
  4. Generic Usenet Account
    Replies:
    8
    Views:
    411
    Generic Usenet Account
    Jan 3, 2004
  5. Gernot Frisch

    erasing from a list within a loop?

    Gernot Frisch, Jul 29, 2004, in forum: C++
    Replies:
    12
    Views:
    550
    Gernot Frisch
    Aug 3, 2004
Loading...

Share This Page