Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   sysread problems (http://www.velocityreviews.com/forums/t858547-sysread-problems.html)

__ __ 07-14-2009 06:23 PM

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!
--
Posted via http://www.ruby-forum.com/.


Tim Hunter 07-14-2009 06:56 PM

Re: sysread problems
 
__ __ 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/.


__ __ 07-14-2009 07:04 PM

Re: sysread problems
 
> 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/.



All times are GMT. The time now is 08:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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