Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8800561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:37:22+00:00 2026-06-14T00:37:22+00:00

I need to create a huge file filled with anything. I’m doing it this

  • 0

I need to create a huge file filled with anything. I’m doing it this way but it takes so long:

exit 1 unless ARGV.length > 0
File.open("file-#{ARGV[0]}M.txt", 'w') do |f| 
  (ARGV[0].to_i*1048576).times {f.write(1) }
end

What’s the best way of doing that (in platform independent way?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:37:24+00:00Added an answer on June 14, 2026 at 12:37 am

    In *nix, use dd:

    system("dd if=/dev/zero of=" + f + " bs=1 count=0 seek=" + ARGV[0] + "M");
    

    If you want some content (instead of zeros) in the file, use

    /dev/random
    

    for if instead of /dev/zero

    If you want a non-sparse file, use

    bs=#{ARGV[0]}M
    

    and omit seek


    Universal method:

    #Create a 1M fill buffer
    fills = '1'*1048576
    File.open("file-#{ARGV[0]}M.txt", 'w') do |f| 
      (ARGV[0].to_i).times {f.write(fills) }
    end
    

    It is similar to the one you have, but it writes 1M at a time. You write 1 byte at a time which creates a lot of overhead for hard disk to search and write. Writing 1M at a time will be much faster. If you have an even faster hard drive (like 16M/s), you can try to increase 1M to 16M.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a huge binary file filled with zeros in Windows OS.
I have a huge XML file with loads of data, I need to create
I have a file which has text like this: #1#14#ADEADE#CAH0F#0#0..... I need to create
I need create clone repository. but I do not know where can I get
I need to create a huge number of files with the following command fsutil
For one off my projects I need to import a very huge text file
I need to create an html based file manager (in rails) with just basic
Huge fan of this website, but it's my first post! I've got an array
I need to create a test database out of a huge database where the
I have a task of creating a script which takes a huge text file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.