Professional Web Applications Themes

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

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 $ ...

  1. #1

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

    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 Guest

  2. #2

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

    >>>>> "A" == Alan Davies <NOSPAMcs96andyahoo.co.ukNOSPAM> 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 Guest

  3. #3

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


    "Alan Davies" <NOSPAMcs96andyahoo.co.ukNOSPAM> schrieb im Newsbeitrag
    news:3fba526dprimark.com...
    > 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."
    [url]http://www.rubycentral.com/book/ref_c_io.html#IO.syswrite[/url]

    robert

    Robert Klemme Guest

  4. #4

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

    >>>>> "R" == Robert Klemme <bob.newsgmx.net> writes:

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

    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 Guest

  5. #5

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

    ts wrote:
    > the problem is in cygwin and linux ...
    Can we get this fixed in 1.8.1 then?
    Alan Davies Guest

  6. #6

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

    Hi,

    At Thu, 20 Nov 2003 18:50:20 +0900,
    ts wrote: 

    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.sysseek(0, IO::SEEK_CUR)}"
     

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

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  7. #7

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

    >>>>> "n" == nobu nokada <net> 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 Guest

  8. #8

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

    > Note that IO#pos is shorthand for seek(0, IO::SEEK_CUR) which 

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

    Alan.
    Alan Guest

Similar Threads

  1. problem in binding xml file data to datagrid xml file isgenerated through JSP file
    By ckshah in forum Macromedia Flex General Discussion
    Replies: 2
    Last Post: May 26th, 03:55 PM
  2. File Viewer / Bloated file sizes / What is the best file format?
    By Martin_Folley@adobeforums.com in forum Adobe Illustrator Windows
    Replies: 1
    Last Post: June 27th, 05:24 PM
  3. Replies: 0
    Last Post: June 13th, 10:13 PM
  4. Open file, make changes, save file, close, re-open, file contents not changed
    By brock@bergdesign.com in forum Adobe Illustrator Macintosh
    Replies: 7
    Last Post: March 7th, 07:29 PM
  5. Replies: 8
    Last Post: July 26th, 04:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139