Create an empty file of certain size

M

Mark Dodwell

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
 
A

ara.t.howard

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://codeforpeople.com/
 
R

Robert Klemme

2008/5/16 Mark Dodwell said:
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
 
H

Heesob Park

Robert said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top