# Ruby File#sysread

**URL:** <https://rubytalk.org/t/ruby-file-sysread/38367>\
**Category:** ruby-talk\
**Created:** [15 June 2007 02:07 UTC](https://rubytalk.org/t/ruby-file-sysread/38367 "2007-06-15T02:07:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aatch](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Aatch](https://rubytalk.org/u/Aatch)\
**Post date:** [15 June 2007 02:07 UTC](https://rubytalk.org/t/ruby-file-sysread/38367/1 "2007-06-15T02:07:55Z")

</div>

I am attempting to create a program that can transfer files. It so far  
can send pure-text files no problem, but it has issues with reading  
anything else. EG. It cut a 6 MB mp3 down to a 6 kb file.

After a bit of fiddling around I found that the program wasn't reading  
the file properly. I'm currently using File#sysread to read the file and  
File#syswrite to write the file. I don't know why it wont read the whole  
file, can some body please help. Here is a copy of a similar program  
with the same problem. (please dont mock it uselessness, it was made to  
try and sort aforementioned problem).

f1 = File.new("BIG.txt")  
f2 = File.new("big.txt", "w")

contents = f1.sysread(f1.stat.size-2)

print f1.stat.size.to\_s + "\n"  
print contents.length.to\_s + "\n"

f2.syswrite contents

print f2.stat.size.to\_s + "\n"

sleep 2

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [15 June 2007 02:20 UTC](https://rubytalk.org/t/ruby-file-sysread/38367/2 "2007-06-15T02:20:33Z")

</div>

Aatch wrote:

> I am attempting to create a program that can transfer files. It so far  
> can send pure-text files no problem, but it has issues with reading  
> anything else. EG. It cut a 6 MB mp3 down to a 6 kb file.  
> &nbsp;&nbsp;  
> On Windows? "rb"

> **···**
>
> --  
> RMagick OS X Installer [[http://rubyforge.org/projects/rmagick/\](http://rubyforge.org/projects/rmagick/%5C)]  
> RMagick Hints & Tips [[http://rubyforge.org/forum/forum.php?forum\_id=1618\](http://rubyforge.org/forum/forum.php?forum_id=1618%5C)]  
> RMagick Installation FAQ [[http://rmagick.rubyforge.org/install-faq.html\](http://rmagick.rubyforge.org/install-faq.html%5C)]

---

<div class="post-metadata">

**Author:** ![Aatch](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Aatch](https://rubytalk.org/u/Aatch)\
**Post date:** [15 June 2007 03:53 UTC](https://rubytalk.org/t/ruby-file-sysread/38367/3 "2007-06-15T03:53:19Z")

</div>

Tim Hunter wrote:

> Aatch wrote:
> 
> > I am attempting to create a program that can transfer files. It so far  
> > can send pure-text files no problem, but it has issues with reading  
> > anything else. EG. It cut a 6 MB mp3 down to a 6 kb file.  
> > &nbsp;&nbsp;  
> > On Windows? "rb"

\<.\< \>.\> yes. I have no other choice. But yes, I was thinking it might be  
windows fault

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [15 June 2007 04:05 UTC](https://rubytalk.org/t/ruby-file-sysread/38367/4 "2007-06-15T04:05:56Z")

</div>

> Tim Hunter wrote:
> 
> > Aatch wrote:
> > 
> > > I am attempting to create a program that can transfer files. It so far  
> > > can send pure-text files no problem, but it has issues with reading  
> > > anything else. EG. It cut a 6 MB mp3 down to a 6 kb file.  
> > > &nbsp;&nbsp;  
> > > On Windows? "rb"
> 
> \<.\< \>.\> yes. I have no other choice. But yes, I was thinking it might be windows fault

Just in case the answer wasn't clear, Windows pretends that "text" files  
are different from "binary" files.

So you'll want to use "binary mode" to read/write binary files on Windows.

Thus, use "rb" instead of "r" to read a binary file, and "wb" instead of "w"  
to write a binary file.

Hope this helps,

Bill

> **···**
>
> From: "James Milner" \<bladeon@gmail.com\>

---

<div class="post-metadata">

**Author:** ![Aatch](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Aatch](https://rubytalk.org/u/Aatch)\
**Post date:** [15 June 2007 04:09 UTC](https://rubytalk.org/t/ruby-file-sysread/38367/5 "2007-06-15T04:09:49Z")

</div>

Bill Kelly wrote:

> From: "James Milner" \<bladeon@gmail.com\>
> 
> > windows fault
> 
> Just in case the answer wasn't clear, Windows pretends that "text" files  
> are different from "binary" files.
> 
> So you'll want to use "binary mode" to read/write binary files on  
> Windows.
> 
> Thus, use "rb" instead of "r" to read a binary file, and "wb" instead of  
> "w"  
> to write a binary file.
> 
> Hope this helps,
> 
> Bill

Ahh, thanks. I've had little experience with the File class in ruby.  
I'll test it now. It should ::prays:: work.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@Nobuyoshi\_Nakada1](https://rubytalk.org/u/Nobuyoshi_Nakada1)\
**Post date:** [17 June 2007 08:35 UTC](https://rubytalk.org/t/ruby-file-sysread/38367/6 "2007-06-17T08:35:27Z")

</div>

Hi,

At Fri, 15 Jun 2007 13:09:49 +0900,  
Aatch Random wrote in [ruby-talk:255684]:

> I'll test it now. It should ::prays:: work.

It's an eternal curse against Windows programers. 🙂

> **···**
>
> --  
> Nobu Nakada
