Sysread problems

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]
i += 1
end

So, how do I get it to work?

Thank you very much!

__ __ 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”.

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!!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs