# Trouble with open/write/close cycle

**URL:** <https://www.ruby-forum.com/t/trouble-with-open-write-close-cycle/86855>\
**Category:** Ruby\
**Created:** [March 24, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/trouble-with-open-write-close-cycle/86855 "2006-03-24T17:42:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/trouble-with-open-write-close-cycle/86855/1 "2006-03-24T17:42:51Z")

</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:

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

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:

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:~\>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/trouble-with-open-write-close-cycle/86855/2 "2006-03-24T17:45:51Z")

</div>

From above:

- f = File.open ‘crapper’, ‘w’

- f = File.open ‘junk’, ‘w’

Sorry.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/trouble-with-open-write-close-cycle/86855/3 "2006-03-24T17:57:53Z")

</div>

On 24-Mar-06, at 11:43 AM, Toby DiPasquale wrote:

> len = 0  
> end  
> 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:

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

x = “012345678\n”  
10.times do  
len = 0  
begin  
len = File.stat( ‘junk’).size  
rescue  
len = 0  
end  
puts len.to\_s  
f = File.open ‘junk’,File::RDWR|File::CREAT  
f.seek len  
f.write x  
f.close  
end

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/trouble-with-open-write-close-cycle/86855/4 "2006-03-24T17:58:55Z")

</div>

> len = File.stat( 'junk').size 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:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/trouble-with-open-write-close-cycle/86855/5 "2006-03-24T18:02:00Z")

</div>

On Mar 24, 2006, at 10:43 AM, Toby DiPasquale wrote:

> begin  
> len = File.stat( ‘junk’).size  
> rescue  
> len = 0  
> end  
> puts len.to\_s  
> f = File.open ‘crapper’, ‘w’  
> f.seek len

The above procedure seems like a complex way to do:

File.open(“junk”, “a”)…

Will that work for you?

James Edward G. II

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/trouble-with-open-write-close-cycle/86855/6 "2006-03-24T18:02:24Z")

</div>

Mike S. 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/trouble-with-open-write-close-cycle/86855/7 "2006-03-24T18:03:35Z")

</div>

James G. 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/trouble-with-open-write-close-cycle/86855/8 "2006-03-24T18:09:48Z")

</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
