File Write bug - Ruby mswin


(Ralph Mason) #1

I have an application that generated a binary file. It has always worked
fine, however having recently switched to the mswin version of ruby I find
that the application nolonger works correctly. The problem is that
file.write (or file.print) is printing an extra byte! (sometimes)

Note :
ruby 1.6.6 (2001-12-26) [i586-mswin32]

Example the code I run is

zones.each{ |i| #write zones
s = i.binstr§
puts “Prewritten line = #{a} pos = #{file.pos} s size = #{s.size}” if a >
35 and a < 45
file.write s
puts "PostWrite line = #{a} pos = #{file.pos} s size = #{s.size} " if a >
35 and a < 45

a+=1
p += i.poi_size()
}

here we just go through a collection writing each member to a file. Each
member is 21 bytes.

Here is the trace from the function

Prewritten line = 39 pos = 843 s size = 21
PostWrite line = 39 pos = 864 s size = 21
Prewritten line = 40 pos = 864 s size = 21
PostWrite line = 40 pos = 885 s size = 21
Prewritten line = 41 pos = 885 s size = 21
PostWrite line = 41 pos = 907 s size = 21
Prewritten line = 42 pos = 907 s size = 21
PostWrite line = 42 pos = 928 s size = 21
Prewritten line = 43 pos = 928 s size = 21
PostWrite line = 43 pos = 949 s size = 21

You see that in line 41, the file offset is at 885 it writes 21 bytes and
the fileoffset goes to 907 - (22 bytes on!)

The trace running on ruby 1.6.5 (2001-09-19) [i386-cygwin] is

Prewritten line = 39 pos = 843 s size = 21
PostWrite line = 39 pos = 864 s size = 21
Prewritten line = 40 pos = 864 s size = 21
PostWrite line = 40 pos = 885 s size = 21
Prewritten line = 41 pos = 885 s size = 21
PostWrite line = 41 pos = 906 s size = 21
Prewritten line = 42 pos = 906 s size = 21
PostWrite line = 42 pos = 927 s size = 21
Prewritten line = 43 pos = 927 s size = 21
PostWrite line = 43 pos = 948 s size = 21

You can see that the byte error does not occur.

Does anyone have any idea what is going on with this? This seems like a
pretty fundamental problem.

Thanks
Ralph


(Yukihiro Matsumoto) #2

Hi,

···

In message “File Write bug - Ruby mswin” on 02/06/18, “Ralph Mason” masonralph@yahoo.com writes:

I have an application that generated a binary file. It has always worked
fine, however having recently switched to the mswin version of ruby I find
that the application nolonger works correctly. The problem is that
file.write (or file.print) is printing an extra byte! (sometimes)

On Windows, you might need to open the file with binary mode. Use
"rb" mode specifier.

						matz.

(U.Nakamura) #3

Hello,

In message “File Write bug - Ruby mswin”

···

on Jun.18,2002 16:23:44, masonralph@yahoo.com wrote:

I have an application that generated a binary file. It has always worked
fine, however having recently switched to the mswin version of ruby I find
that the application nolonger works correctly. The problem is that
file.write (or file.print) is printing an extra byte! (sometimes)

How do you open the file?
If you use open(file, “w”), change it to open(file, “wb”).

Regards,

U.Nakamura usa@osb.att.ne.jp


(Ralph Mason) #4

Thanks matz,

You are exactly right - the string being written had a \n in it.

File.new (“wb”) fixed the problem.

Ralph

···

----- Original Message -----
From: “Yukihiro Matsumoto” matz@ruby-lang.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, June 18, 2002 7:28 PM
Subject: Re: File Write bug - Ruby mswin

Hi,

In message “File Write bug - Ruby mswin” > on 02/06/18, “Ralph Mason” masonralph@yahoo.com writes:

I have an application that generated a binary file. It has always worked
fine, however having recently switched to the mswin version of ruby I
find

that the application nolonger works correctly. The problem is that
file.write (or file.print) is printing an extra byte! (sometimes)

On Windows, you might need to open the file with binary mode. Use
"rb" mode specifier.

matz.