# Sysread problems

**URL:** <https://www.ruby-forum.com/t/sysread-problems/172099>\
**Category:** Ruby\
**Created:** [July 14, 2009, 8:22pm UTC](https://www.ruby-forum.com/t/sysread-problems/172099 "2009-07-14T20:22:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![guraz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a9adbd/32.png) [@guraz](https://www.ruby-forum.com/u/guraz)\
**Post date:** [July 14, 2009, 8:22pm UTC](https://www.ruby-forum.com/t/sysread-problems/172099/1 "2009-07-14T20:22:58Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![guraz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a9adbd/32.png) [@guraz](https://www.ruby-forum.com/u/guraz)\
**Post date:** [July 14, 2009, 8:56pm UTC](https://www.ruby-forum.com/t/sysread-problems/172099/2 "2009-07-14T20:56:08Z")

</div>

\_\_ \_\_ 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”.

---

<div class="post-metadata">

**Author:** ![guraz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a9adbd/32.png) [@guraz](https://www.ruby-forum.com/u/guraz)\
**Post date:** [July 14, 2009, 9:03pm UTC](https://www.ruby-forum.com/t/sysread-problems/172099/3 "2009-07-14T21:03:55Z")

</div>

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