# File.open not reading full file when repeatedly called

**URL:** <https://www.ruby-forum.com/t/file-open-not-reading-full-file-when-repeatedly-called/58792>\
**Category:** Ruby\
**Created:** [April 25, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/file-open-not-reading-full-file-when-repeatedly-called/58792 "2006-04-25T23:31:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [April 25, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/file-open-not-reading-full-file-when-repeatedly-called/58792/1 "2006-04-25T23:31:13Z")

</div>

I have a trange situation…

I have a read method which uses the syntax:  
data = File.open(myFile)  
data.each\_line { |line|  
# read in the file  
}  
data.close

The file is 6311 lines long.

If I read in file\_A and write it out to file\_B I get a duplicate as  
expected.  
BUT if I, WITHOUT exiting the program read in file\_B it will only read  
in 6250 lines.

If I exit the program and restart with reading in file\_B the entire file  
is read.

Is this a memory problem with ruby? Or should I be doing something  
different when I read a file?

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [April 25, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/file-open-not-reading-full-file-when-repeatedly-called/58792/2 "2006-04-25T23:46:14Z")

</div>

On Apr 25, 2006, at 5:31 PM, Todd S. wrote:

> The file is 6311 lines long.  
> Is this a memory problem with ruby? Or should I be doing something  
> different when I read a file?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Could you give more of your code please? (Also if you just want to  
read the whole file into one string you can do File.read(“filename”) )

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [April 25, 2006, 11:49pm UTC](https://www.ruby-forum.com/t/file-open-not-reading-full-file-when-repeatedly-called/58792/3 "2006-04-25T23:49:19Z")

</div>

Todd S. wrote:

> The file is 6311 lines long.  
> different when I read a file?  
> You need to flush the file buffer before you start reading.

You can do this either by closing the file\_B after you have completed  
writing it and then opening for reading, or by explicitly calling flush  
on file\_B.

–

Ray

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [April 26, 2006, 12:48am UTC](https://www.ruby-forum.com/t/file-open-not-reading-full-file-when-repeatedly-called/58792/4 "2006-04-26T00:48:30Z")

</div>

I completely forgot to put the myFile.close call into the write routine.  
Because the read was not getting all the data, that’s where I was  
looking for the problem.

Thanks both for your help. Following up on your answers lead me to the  
solution.

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [April 26, 2006, 11:47am UTC](https://www.ruby-forum.com/t/file-open-not-reading-full-file-when-repeatedly-called/58792/5 "2006-04-26T11:47:26Z")

</div>

2006/4/26, Todd S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I completely forgot to put the myFile.close call into the write routine.

That’s the reason why it’s recommended to use the block form of  
File.open().

Cheers

robert
