Weird behavior with IO#read

Discussion in 'Ruby' started by Lennon Day-Reynolds, Aug 25, 2004.

  1. Okay, maybe I just need to take a break from coding for a couple of
    hours, but I can't for the life of me figure out what's going wrong
    with this code:

    --
    BLOCKSIZE=1024*256
    open('testdata.in') do |input|
    open('testdata.out') do |output|
    while (chunk = input.read(BLOCKSIZE)) do
    output.write(chunk)
    end
    end
    end
    --

    On Linux with 1.8.2-preview2, it works as expected (and much faster
    than I anticipated, which is good). On Windows XP using the latest
    one-click, it terminates after a single iteration through the 'while'
    loop. Changing the blocksize doesn't make a difference; neither does
    using a different looping construct.

    It seems like this is a bug in the Ruby interpreter, but it also seems
    so basic as to be unlikely to have slipped under the radar in testing.
    Can anyone else confirm this behavior? I'll try again in a few hours
    when I get home, but would like to stop banging my head against the
    problem sooner than that, if possible.


    --
    Lennon
    rcoder.net
     
    Lennon Day-Reynolds, Aug 25, 2004
    #1
    1. Advertising

  2. Lennon Day-Reynolds

    Bill Kelly Guest

    Hi,

    From: "Lennon Day-Reynolds" <>
    >
    > Okay, maybe I just need to take a break from coding for a couple of
    > hours, but I can't for the life of me figure out what's going wrong
    > with this code:
    >
    > --
    > BLOCKSIZE=1024*256
    > open('testdata.in') do |input|
    > open('testdata.out') do |output|
    > while (chunk = input.read(BLOCKSIZE)) do
    > output.write(chunk)
    > end
    > end
    > end
    > --
    >
    > On Linux with 1.8.2-preview2, it works as expected (and much faster
    > than I anticipated, which is good). On Windows XP using the latest
    > one-click, it terminates after a single iteration through the 'while'
    > loop. Changing the blocksize doesn't make a difference; neither does
    > using a different looping construct.


    Is there binary data in the files? If so try opening them
    in binary mode ("rb" and "wb")... Windows sees ^Z as end of
    file in "text mode"... an artifact stupidly inherited from
    a filesystem that needed it because it only measured file
    sizes in blocks and didn't know where the precise end of file
    was without the marker character. ("Stupidly inherited"
    because windows has never needed that to know where the true
    end of file is.)


    Hope this helps,

    Regards,

    Bill
     
    Bill Kelly, Aug 25, 2004
    #2
    1. Advertising

  3. Lennon Day-Reynolds

    Mike Hall Guest

    Lennon Day-Reynolds wrote:

    > while (chunk = input.read(BLOCKSIZE)) do


    Too much C programming? I thought that we "shouldn't" use tests like these
    in Ruby -- in other words, we should test 'chunk' against 'nil' or emptyness
    or something. But then, you say it works OK on one platform...
     
    Mike Hall, Aug 25, 2004
    #3
  4. Thank you, Bill. You're right -- I was opening the files in text mode.

    See, I had just been coding for too long.

    Thanks again.


    --
    Lennon
    rcoder.net
     
    Lennon Day-Reynolds, Aug 25, 2004
    #4
    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. dorayme
    Replies:
    1
    Views:
    624
    richard
    Jan 21, 2011
  2. richard
    Replies:
    0
    Views:
    587
    richard
    Jan 21, 2011
  3. richard
    Replies:
    0
    Views:
    618
    richard
    Jan 21, 2011
  4. Beauregard T. Shagnasty

    Re: A Weird Appearance for a Weird Site

    Beauregard T. Shagnasty, Jan 21, 2011, in forum: HTML
    Replies:
    1
    Views:
    440
    Captain Paralytic
    Jan 21, 2011
  5. David Segall
    Replies:
    0
    Views:
    634
    David Segall
    Jan 22, 2011
Loading...

Share This Page