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

So, how do I get it to work?

Thank you very much!
 
T

Tim Hunter

__ __ said:
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!!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top