[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Create an empty file of certain size

Mark Dodwell

5/15/2008 10:10:00 PM

Hi,

I need to create an empty (or at least garbage) file of a certain size.
I'm doing it the way below, but it is rather slow -- any ideas on a
quicker way?

File.open("tmp.txt", 'w') { |f| 10.megabytes.times { f.write "a" } }

Thanks!

~ Mark
--
Posted via http://www.ruby-....

4 Answers

ara.t.howard

5/15/2008 10:25:00 PM

0


On May 15, 2008, at 4:09 PM, Mark Dodwell wrote:

> Hi,
>
> I need to create an empty (or at least garbage) file of a certain
> size.
> I'm doing it the way below, but it is rather slow -- any ideas on a
> quicker way?
>
> File.open("tmp.txt", 'w') { |f| 10.megabytes.times { f.write "a" } }
>
> Thanks!
>
> ~ Mark

fd.truncate( 2 ** 20 )

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Mark Dodwell

5/15/2008 10:44:00 PM

0

> fd.truncate( 2 ** 20 )

Thanks that's great, didn't know about that method!
--
Posted via http://www.ruby-....

Robert Klemme

5/16/2008 7:22:00 AM

0

2008/5/16 Mark Dodwell <seo@mkdynamic.co.uk>:
>> fd.truncate( 2 ** 20 )
>
> Thanks that's great, didn't know about that method!

If it does not work on your platform, you can speed up your solution
by writing larger chunks:

BUFFER = ("a" * 1024).freeze
File.open("tmp.txt", 'wb') { |f| 10.kilobytes.times { f.write BUFFER } }

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Heesob Park

5/16/2008 7:50:00 AM

0

Robert Klemme wrote:
> 2008/5/16 Mark Dodwell <seo@mkdynamic.co.uk>:
>>> fd.truncate( 2 ** 20 )
>>
>> Thanks that's great, didn't know about that method!
>
> If it does not work on your platform, you can speed up your solution
> by writing larger chunks:
>
> BUFFER = ("a" * 1024).freeze
> File.open("tmp.txt", 'wb') { |f| 10.kilobytes.times { f.write BUFFER } }
>
Here is another way:

size = 2**30
File.open("tmp.txt","wb"){|f|f.seek(size-1);f.write("\0")}

Regards,

Park Heesob
--
Posted via http://www.ruby-....