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
on 16.05.2008 00:09
on 16.05.2008 00:25
On May 15, 2008, at 4:09 PM, Mark Dodwell wrote: > > ~ Mark fd.truncate( 2 ** 20 ) a @ http://codeforpeople.com/
on 16.05.2008 00:43
> fd.truncate( 2 ** 20 )
Thanks that's great, didn't know about that method!
on 16.05.2008 09:22
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
on 16.05.2008 09:49
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