# Trouble with open/write/close cycle

**URL:** <https://rubytalk.org/t/trouble-with-open-write-close-cycle/26077>\
**Category:** ruby-talk\
**Created:** [24 March 2006 16:43 UTC](https://rubytalk.org/t/trouble-with-open-write-close-cycle/26077 "2006-03-24T16:43:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobias\_DiPasquale](https://avatars.discourse-cdn.com/v4/letter/t/958977/32.png) [@Tobias\_DiPasquale](https://rubytalk.org/u/Tobias_DiPasquale)\
**Post date:** [24 March 2006 16:43 UTC](https://rubytalk.org/t/trouble-with-open-write-close-cycle/26077/1 "2006-03-24T16:43:08Z")

</div>

Hi all,

I'm having trouble getting an open/write/close cycle to actually put the  
correct data in the file. Here's some code that illustrates the problem:

\<snip\>  
x = "012345678\n"  
10.times do  
&nbsp;&nbsp;len = 0  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;len = File.stat( 'junk').size  
&nbsp;&nbsp;rescue  
&nbsp;&nbsp;&nbsp;&nbsp;len = 0  
&nbsp;&nbsp;end  
&nbsp;&nbsp;puts len.to\_s  
&nbsp;&nbsp;f = File.open 'crapper', 'w'  
&nbsp;&nbsp;f.seek len  
&nbsp;&nbsp;f.write x  
&nbsp;&nbsp;f.close  
end  
\</snip\>

produces:

0  
10  
20  
30  
40  
50  
60  
70  
80  
90

as output. However, the file 'junk' is filled with zeroes, except for  
the last 10 bytes, which are what you'd expect them to be:

\<snip\>  
puma:~\> od -h junk  
0000000 0000 0000 0000 0000 0000 0000 0000 0000

> **···**
>
> \*  
> 0000120 0000 0000 0000 0000 0000 3130 3332 3534  
> 0000140 3736 0a38  
> 0000144  
> puma:~\>  
> \</snip\>
> 
> Anyone know what I'm doing wrong? I tried using sysseek and syswrite,  
> but I got the same results. Is Ruby's internal buffering screwing me up  
> here somehow? TIA.
> 
> --  
> Toby DiPasquale
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Tobias\_DiPasquale](https://avatars.discourse-cdn.com/v4/letter/t/958977/32.png) [@Tobias\_DiPasquale](https://rubytalk.org/u/Tobias_DiPasquale)\
**Post date:** [24 March 2006 16:46 UTC](https://rubytalk.org/t/trouble-with-open-write-close-cycle/26077/2 "2006-03-24T16:46:08Z")

</div>

From above:

- f = File.open 'crapper', 'w'  
+ f = File.open 'junk', 'w'

Sorry.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mike\_Stok1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/mike_stok1/32/2065_2.png) [@Mike\_Stok1](https://rubytalk.org/u/Mike_Stok1)\
**Post date:** [24 March 2006 16:57 UTC](https://rubytalk.org/t/trouble-with-open-write-close-cycle/26077/3 "2006-03-24T16:57:47Z")

</div>

opening a file for writing truncates it. Does this, opening it for reading & writing, do what you want?

x = "012345678\n"  
10.times do  
&nbsp;&nbsp;&nbsp;len = 0  
&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len = File.stat( 'junk').size  
&nbsp;&nbsp;&nbsp;rescue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len = 0  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;puts len.to\_s  
&nbsp;&nbsp;&nbsp;f = File.open 'junk',File::RDWR|File::CREAT  
&nbsp;&nbsp;&nbsp;f.seek len  
&nbsp;&nbsp;&nbsp;f.write x  
&nbsp;&nbsp;&nbsp;f.close  
end

Mike

> **···**
>
> On 24-Mar-06, at 11:43 AM, Toby DiPasquale wrote:
> 
> > Hi all,
> > 
> > I'm having trouble getting an open/write/close cycle to actually put the  
> > correct data in the file. Here's some code that illustrates the problem:
> > 
> > \<snip\>  
> > x = "012345678\n"  
> > 10.times do  
> > &nbsp;&nbsp;len = 0  
> > &nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;len = File.stat( 'junk').size  
> > &nbsp;&nbsp;rescue  
> > &nbsp;&nbsp;&nbsp;&nbsp;len = 0  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;puts len.to\_s  
> > &nbsp;&nbsp;f = File.open 'crapper', 'w'  
> > &nbsp;&nbsp;f.seek len  
> > &nbsp;&nbsp;f.write x  
> > &nbsp;&nbsp;f.close  
> > end  
> > \</snip\>
> > 
> > produces:
> > 
> > 0  
> > 10  
> > 20  
> > 30  
> > 40  
> > 50  
> > 60  
> > 70  
> > 80  
> > 90
> > 
> > as output. However, the file 'junk' is filled with zeroes, except for  
> > the last 10 bytes, which are what you'd expect them to be:
> 
> --
> 
> Mike Stok \<mike@stok.ca\>  
> [http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)
> 
> The "`Stok' disclaimers" apply.

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [24 March 2006 16:58 UTC](https://rubytalk.org/t/trouble-with-open-write-close-cycle/26077/4 "2006-03-24T16:58:52Z")

</div>

> \<snip\>  
> &nbsp;&nbsp;&nbsp;&nbsp;len = File.stat( 'junk').size  
> \<snip\>  
> &nbsp;&nbsp;f = File.open 'crapper', 'w'

Are there really 2 files? or is this a typo?

The docs on IO::open says tha using the 'w' mode cause the file to be  
truncated if it exists.  
You need to look at 'a' mode

cheers

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [24 March 2006 17:01 UTC](https://rubytalk.org/t/trouble-with-open-write-close-cycle/26077/5 "2006-03-24T17:01:47Z")

</div>

The above procedure seems like a complex way to do:

File.open("junk", "a")...

Will that work for you?

James Edward Gray II

> **···**
>
> On Mar 24, 2006, at 10:43 AM, Toby DiPasquale wrote:
> 
> > &nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;len = File.stat( 'junk').size  
> > &nbsp;&nbsp;rescue  
> > &nbsp;&nbsp;&nbsp;&nbsp;len = 0  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;puts len.to\_s  
> > &nbsp;&nbsp;f = File.open 'crapper', 'w'  
> > &nbsp;&nbsp;f.seek len

---

<div class="post-metadata">

**Author:** ![Tobias\_DiPasquale](https://avatars.discourse-cdn.com/v4/letter/t/958977/32.png) [@Tobias\_DiPasquale](https://rubytalk.org/u/Tobias_DiPasquale)\
**Post date:** [24 March 2006 17:03 UTC](https://rubytalk.org/t/trouble-with-open-write-close-cycle/26077/6 "2006-03-24T17:03:39Z")

</div>

Mike Stok wrote:

> opening a file for writing truncates it.

ARRRGH! I'm a moron. Of course, you are right. Thanks for the mental  
clue-by-four.

> **···**
>
> --  
> Toby DiPasquale
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Tobias\_DiPasquale](https://avatars.discourse-cdn.com/v4/letter/t/958977/32.png) [@Tobias\_DiPasquale](https://rubytalk.org/u/Tobias_DiPasquale)\
**Post date:** [24 March 2006 17:03 UTC](https://rubytalk.org/t/trouble-with-open-write-close-cycle/26077/7 "2006-03-24T17:03:52Z")

</div>

James Gray wrote:

> The above procedure seems like a complex way to do:
> 
> File.open("junk", "a")...
> 
> Will that work for you?

No, the above code was just a small example to illustrate the issue. The  
real code does lots more before and after each write.

> **···**
>
> --  
> Toby DiPasquale
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [24 March 2006 17:08 UTC](https://rubytalk.org/t/trouble-with-open-write-close-cycle/26077/8 "2006-03-24T17:08:52Z")

</div>

Toby DiPasquale wrote:

> From above:
> 
> - f = File.open 'crapper', 'w'  
> + f = File.open 'junk', 'w'
> 
> Sorry.

Not a problem.  
When I run your snippet i get:

> ruby -w fwrite-test.rb

0  
11  
22  
33  
44  
55  
66  
77  
88  
99

It looks like seeking on an empty file fills it with null data. Not  
sure if thats a Ruby thing or a feature of the underlying IO libs.

change the 'w' to an 'a' (an delete the current 'junk' file)  
and the file produced looks like:  
012345678  
012345678  
012345678  
012345678  
012345678  
012345678  
012345678  
012345678  
012345678  
012345678

cheers
