Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Erasing text from a file

Reply
Thread Tools

Erasing text from a file

 
 
Lucas L.
Guest
Posts: n/a
 
      05-11-2008
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/.

 
Reply With Quote
 
 
 
 
Andrea Fazzi
Guest
Posts: n/a
 
      05-11-2008
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 }


 
Reply With Quote
 
 
 
 
Lucas L.
Guest
Posts: n/a
 
      05-11-2008
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/.

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      05-11-2008
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
Andrea Fazzi
Guest
Posts: n/a
 
      05-11-2008
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


 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      05-11-2008
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


 
Reply With Quote
 
Lucas L.
Guest
Posts: n/a
 
      05-12-2008
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Erasing File Data Bart Bailey Computer Security 28 05-18-2007 05:42 PM
Proper way to write a CLEAN xml file (erasing old data first) darrel ASP .Net 4 05-05-2006 06:57 PM
Erasing unerasable file. Roger1 Computer Support 7 08-22-2005 08:35 PM
Missing dll file as result of erasing Hotbar Viv Computer Support 4 01-02-2005 01:26 AM
Re: Error Download file from TFTP after erasing Flash ( Cisco 2610) Rainer Temme Cisco 2 07-09-2003 02:17 AM



Advertisments