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

  • Home
  • SEARCH
  • 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 8316769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:26:02+00:00 2026-06-08T21:26:02+00:00

After creating a file and populating data into it, before close, need read part

  • 0

After creating a file and populating data into it, before close, need read part data and
calculate the checksum. The issue is you cant read the data before close the file. Code
snippet is as follows.

My question is how to create a file, write data, read part of the file, then close it? One
possible solution is using a buffer before write to the file, but it is not convenient if
the file is big, such as MB, GB, TB, PB.

  begin
  File.open(@f_name,"w+") do |file|

    @f_old_size.times do
      file.write "1"
    end

    file.flush
    file.sync

    #################
    # read file fails
    # before close
    #################
    while line = file.gets
      puts line
    end

  end
  rescue => err
   puts "Exception: #{err}"
  end

  #####################
  # read file successfully
  # after close it
  #####################
  File.open(@f_name,"r") do |file|
    line = file.gets
    puts line
  end
  • 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-08T21:26:03+00:00Added an answer on June 8, 2026 at 9:26 pm

    The problem that you’re running into is that Ruby IO reads through the file and keeps track of where in the file it is. After you’ve written out your data, the ‘seek head’ of the IO object is at the bottom of the file. When you ask it for the next line, because it’s at the bottom, you don’t get anything.

    If you change your code to include a file.rewind as such, it works:

    #################
    # read file fails
    # before close
    #################
    file.rewind  # <--  THIS IS THE ADDED LINE
    while line = file.gets
      puts line
    end
    

    The #rewind method sets the ‘seek head’ back to the beginning of the file, which is what you’re looking to do.

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

Sidebar

Related Questions

After creating a PDF file in PHP Using DOMPDF, I need to redirect user
after creating a file and populating it with that with a thread if the
After creating a new form, I usually perform this ritual: Change the name into
After creating a listview from VS2010 and adding it as a web part to
After creating a batch file in visual studio, I get an error when I
Can I delete the DLL-File C:\PATH\TO\DLL\FILE.DLL after creating an assembly with CREATE ASSEMBLY MyAssemblyName
I ran into a problem after creating a screensaver that requires writing to a
After creating settings for each dll , a .dll.config file is generated. If that
I have created icns file using IconFamily, but after creating the image the quality
I have a php/action.php. That script creats a file in the folder ../data/myfile.kml. After

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.