Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [BUG] File#rewind, File#syswrite, File#pos on Cygwin build

Reply
Thread Tools

[BUG] File#rewind, File#syswrite, File#pos on Cygwin build

 
 
Alan Davies
Guest
Posts: n/a
 
      11-18-2003
On the cygwin build of ruby v1.8.0, I have encountered a strange bug
when using rewind, syswrite and pos. If you open a file in read/write
mode, read the contents, rewind, syswrite some data, then File#pos
always seems to return zero. This does not happen if you use the
windows build, or you replace 'syswrite' with 'write'.

e.g:

$ cat syswrite.rb
#!/bin/ruby

testStr = "hello\nthis is some example text\nblah blah blah"

# read it, rewind, then write it back again
File.open("out.txt", 'r+') do |file|
file.readlines
file.rewind
bytes = file.syswrite(testStr)
puts "#{bytes} bytes written"
puts "Now at position #{file.pos}"
end

$ ls > out.txt

$ syswrite.rb
46 bytes written
Now at position 0
 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      11-18-2003
>>>>> "A" == Alan Davies <(E-Mail Removed)> writes:

A> On the cygwin build of ruby v1.8.0, I have encountered a strange bug
A> when using rewind, syswrite and pos. If you open a file in read/write
A> mode, read the contents, rewind, syswrite some data, then File#pos
A> always seems to return zero. This does not happen if you use the
A> windows build, or you replace 'syswrite' with 'write'.

try to add a flush

A> File.open("out.txt", 'r+') do |file|
A> file.readlines
A> file.rewind
A> bytes = file.syswrite(testStr)

file.flush

A> puts "#{bytes} bytes written"
A> puts "Now at position #{file.pos}"
A> end


Guy Decoux



 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      11-20-2003

"Alan Davies" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On the cygwin build of ruby v1.8.0, I have encountered a strange bug
> when using rewind, syswrite and pos. If you open a file in read/write
> mode, read the contents, rewind, syswrite some data, then File#pos
> always seems to return zero. This does not happen if you use the
> windows build, or you replace 'syswrite' with 'write'.


"Do not mix with other methods that write to ios or you may get
unpredictable results."
http://www.rubycentral.com/book/ref_...ml#IO.syswrite

robert

 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      11-20-2003
>>>>> "R" == Robert Klemme <(E-Mail Removed)> writes:

R> "Do not mix with other methods that write to ios or you may get
R> unpredictable results."
R> http://www.rubycentral.com/book/ref_...ml#IO.syswrite

No, no.

moulon% cat b.rb
#!/usr/bin/ruby
testStr = "hello\nthis is some example text\nblah blah blah"

File.open("out.txt", 'r+') do |file|
file.readlines
puts "Now at position #{file.pos}"
file.rewind
puts "Now at position #{file.pos}"
bytes = file.syswrite(testStr)
puts "#{bytes} bytes written"
puts "Now at position #{file.pos}"
end
moulon%

moulon% ruby -v b.rb
ruby 1.8.0 (2003-08-04) [sparc-solaris2.7]
Now at position 3330
Now at position 0
46 bytes written
Now at position 46
moulon%

the problem is in cygwin and linux ...



Guy Decoux


 
Reply With Quote
 
Alan Davies
Guest
Posts: n/a
 
      11-21-2003
ts wrote:
> the problem is in cygwin and linux ...


Can we get this fixed in 1.8.1 then?
 
Reply With Quote
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      11-27-2003
Hi,

At Thu, 20 Nov 2003 18:50:20 +0900,
ts wrote:
> R> "Do not mix with other methods that write to ios or you may get
> R> unpredictable results."
> R> http://www.rubycentral.com/book/ref_...ml#IO.syswrite
>
> No, no.


Robert is correct.

Note that IO#pos is shorthand for seek(0, IO::SEEK_CUR) which
an interface for fseek, that means it is buffered IO routine.
You have to use #sysseek instead.

> puts "Now at position #{file.pos}"

puts "Now at position #{file.sysseek(0, IO::SEEK_CUR)}"

> moulon% ruby -v b.rb
> ruby 1.8.0 (2003-08-04) [sparc-solaris2.7]
> Now at position 3330
> Now at position 0
> 46 bytes written
> Now at position 46
> moulon%
>
> the problem is in cygwin and linux ...


This behavior is not guaranteed. And also, once stdio layer
operation is done, whether sysio works well or not is unknown.

--
Nobu Nakada


 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      11-27-2003
>>>>> "n" == nobu nokada <(E-Mail Removed)> writes:

n> This behavior is not guaranteed. And also, once stdio layer
n> operation is done, whether sysio works well or not is unknown.

we just don't work on the same systems :


When a file is opened with update mode (+ as the second or
third character in the mode argument), both input and output
may be performed on the associated stream. However, output
must not be directly followed by input without an interven-
ing call to fflush(3C) or to a file positioning function (
fseek(3C), fsetpos(3C) or rewind(3C)), and input must not be
directly followed by output without an intervening call to a
file positioning function, unless the input operation
encounters end-of-file.


Guy Decoux


 
Reply With Quote
 
Alan Davies
Guest
Posts: n/a
 
      11-27-2003
> Note that IO#pos is shorthand for seek(0, IO::SEEK_CUR) which
> an interface for fseek, that means it is buffered IO routine.
> You have to use #sysseek instead.


Ah that explains a lot. Why doesn't #syspos exist then?

Alan.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SWsoft Acronis Disk Director Suite 9.0 Build 508, Acronis OS Selector 8.0 Build 917, Acronis Partition Expert 2003 Build 292, Acronis Power Utilities 2004 Build 502, F-SECURE.ANTI vIRUS.PROXY v1.10.17.WINALL, F-SECURE.ANTI vIRUS v5.50.10260 for CITRI vvcd Computer Support 0 09-25-2004 01:38 AM
Python 2.3.3 : Win32 build vs Cygwin build performance ? Nicolas Lehuen Python 3 01-28-2004 07:30 AM
WELCOME to cygwin@cygwin.com cygwin-help@cygwin.com Python 1 09-05-2003 07:46 AM
confirm unsubscribe from cygwin-announce@cygwin.com cygwin-announce-help@cygwin.com Python 0 09-05-2003 01:29 AM
confirm unsubscribe from cygwin@cygwin.com cygwin-help@cygwin.com Python 0 09-04-2003 06:34 PM



Advertisments