Trouble with open/write/close cycle

T

Toby DiPasquale

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

Toby DiPasquale

From above:

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

Sorry.
 
C

ChrisH

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

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

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

cheers
 
J

James Edward Gray II

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 Gray II
 
T

Toby DiPasquale

James said:
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.
 
C

ChrisH

Toby said:
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
 

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top