From: Mike Stok Date: 2006-03-25T01:57:47+09:00 Subject: Re: Trouble with open/write/close cycle 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: > > > 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: 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 Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.