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

Discussion in 'Ruby' started by Alan Davies, Nov 18, 2003.

  1. Alan Davies

    Alan Davies Guest

    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
    Alan Davies, Nov 18, 2003
    #1
    1. Advertising

  2. Alan Davies

    ts Guest

    >>>>> "A" == Alan Davies <> 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
    ts, Nov 18, 2003
    #2
    1. Advertising

  3. "Alan Davies" <> schrieb im Newsbeitrag
    news:...
    > 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_c_io.html#IO.syswrite

    robert
    Robert Klemme, Nov 20, 2003
    #3
  4. Alan Davies

    ts Guest

    >>>>> "R" == Robert Klemme <> 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_c_io.html#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
    ts, Nov 20, 2003
    #4
  5. Alan Davies

    Alan Davies Guest

    ts wrote:
    > the problem is in cygwin and linux ...


    Can we get this fixed in 1.8.1 then?
    Alan Davies, Nov 21, 2003
    #5
  6. Alan Davies

    Guest

    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_c_io.html#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
    , Nov 27, 2003
    #6
  7. Alan Davies

    ts Guest

    >>>>> "n" == nobu nokada <> 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
    ts, Nov 27, 2003
    #7
  8. Alan Davies

    Alan Davies Guest

    > 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.
    Alan Davies, Nov 27, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    2,395
  2. Nicolas Lehuen
    Replies:
    3
    Views:
    702
    Nicolas Lehuen
    Jan 28, 2004
  3. Rajorshi  Biswas
    Replies:
    3
    Views:
    361
    Rajorshi Biswas
    Jul 20, 2006
  4. rainny
    Replies:
    2
    Views:
    473
    Roedy Green
    Mar 9, 2009
  5. prati
    Replies:
    0
    Views:
    428
    prati
    Oct 27, 2012
Loading...

Share This Page