sysread problems

Discussion in 'Ruby' started by __ __, Jul 14, 2009.

  1. __ __

    __ __ Guest

    I want to open a file and store its contents in an array. When I read
    the documentation for sysread, it seemed to be just what I wanted.
    However, when I actually run my code, it returns only part of the file
    successfully. It seems the amount of the file returned successfully
    varies with the file, but it is always vastly smaller than the file
    itself, and it always starts at the beginning of the file. For example,
    with a file about 1 MB in size, the amount sysread returned successfully
    was about 500 bytes. Sorry if I missed something simple, as I am new to
    Ruby. Here is my code:

    # This is intended to open a file and put its contents into the array
    'content'.
    aFile = File.new($filename, "r")
    size = File.size?($filename)
    content=[]
    content=aFile.sysread(size)
    aFile.close

    # This is intended to show whether the array 'content' really has all
    the contents of the file.
    i = 0
    while i < size
    puts content
    i += 1
    end

    So, how do I get it to work?

    Thank you very much!
    --
    Posted via http://www.ruby-forum.com/.
    __ __, Jul 14, 2009
    #1
    1. Advertising

  2. __ __

    Tim Hunter Guest

    __ __ wrote:
    > I want to open a file and store its contents in an array. When I read
    > the documentation for sysread, it seemed to be just what I wanted.
    > However, when I actually run my code, it returns only part of the file
    > successfully. It seems the amount of the file returned successfully
    > varies with the file, but it is always vastly smaller than the file
    > itself, and it always starts at the beginning of the file. For example,
    > with a file about 1 MB in size, the amount sysread returned successfully
    > was about 500 bytes.


    If you're running on Windows, and the files you're reading contain
    binary data (that is, not plain text), then I'm guessing that Ruby is
    encountering a ^Z character and interpreting it as EOF. You can fix this
    by opening the file in binary mode, i.e. "rb" instead of plain "r".

    --
    Posted via http://www.ruby-forum.com/.
    Tim Hunter, Jul 14, 2009
    #2
    1. Advertising

  3. __ __

    __ __ Guest

    > If you're running on Windows, and the files you're reading contain
    > binary data (that is, not plain text), then I'm guessing that Ruby is
    > encountering a ^Z character and interpreting it as EOF. You can fix this
    > by opening the file in binary mode, i.e. "rb" instead of plain "r".


    Wow, thank you!! It works perfectly now!!
    --
    Posted via http://www.ruby-forum.com/.
    __ __, Jul 14, 2009
    #3
    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. Hal Fulton

    sysread and buffered I/O

    Hal Fulton, Jul 21, 2004, in forum: Ruby
    Replies:
    39
    Views:
    619
    Tanaka Akira
    Jul 24, 2004
  2. William E. Rubin
    Replies:
    3
    Views:
    106
    William E. Rubin
    Dec 8, 2005
  3. Muazzam Mushtaq
    Replies:
    0
    Views:
    353
    Muazzam Mushtaq
    Mar 28, 2006
  4. pihentagy

    IO#sysread on windows

    pihentagy, Jun 14, 2006, in forum: Ruby
    Replies:
    3
    Views:
    159
    pihentagy
    Jun 14, 2006
  5. Mento Ruby
    Replies:
    0
    Views:
    123
    Mento Ruby
    Dec 11, 2006
Loading...

Share This Page