# Strange read behaviour on windows

**URL:** <https://www.ruby-forum.com/t/strange-read-behaviour-on-windows/72293>\
**Category:** Ruby\
**Created:** [October 16, 2006, 8:41am UTC](https://www.ruby-forum.com/t/strange-read-behaviour-on-windows/72293 "2006-10-16T08:41:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [October 16, 2006, 8:41am UTC](https://www.ruby-forum.com/t/strange-read-behaviour-on-windows/72293/1 "2006-10-16T08:41:47Z")

</div>

This is my first post to the ruby mailing lists, so let me first thank  
all the people who’s wonderful work I’ve made use of – great stuff!.

I’ve been trying to upload some large files, using  
Net::HTTP::Put#body\_stream=, but have ran into some problems.

Most of my initial testing was in cygwin, where most things “just work”.  
upon trying it in the mingw32 build, i got https errors due to openssl  
doing some non-block fcntls (IO#fcntl “not implemented on this  
platform”).

I switched to the i386-mswin32, but now I getting some more fundamental  
problems, when doing file reads. I’m typically getting reads back under  
the requested size, (not a problem), but this is generating premature  
EOF errors. If I just use File#read, I correctly get the entire file.

An irb snippet says it best:

irb(main):139:0\> f.pos  
=\> 1605632  
irb(main):140:0\> f.read 1024  
=\> nil  
irb(main):141:0\> f.pos  
=\> 1605632  
irb(main):142:0\> f.read 1024  
=\> nil  
irb(main):143:0\> f.sysread 1024  
=\>  
“R\326ER6qJ\302So\233\321\373\25218\205\260u\*\245\212`k\246\300Vo\233\311\373 \252)8\222}z\367\270g58\026x\022q" irb(main):144:0> f.pos => 1606656 irb(main):145:0> f.sysread 1024 => "\340G\370\023\3023\b:R\264C\225\326\213\276\335\244\234O)N\035$3N\307\017\36 0d\261\321A\245\334\250y|\025lo\253\306*\205\316\246\276$`\366\022\255”  
irb(main):146:0\> \_.length  
=\> 52  
irb(main):147:0\> f.sysread 1024  
EOFError: end of file reached  
from (irb):147:in `sysread’  
from (irb):147  
from :0  
irb(main):148:0\> f.pos  
=\> 1607680  
irb(main):149:0\> RUBY\_PLATFORM  
=\> “i386-mswin32”

(FWIW, the file f in question, is just the one-click installer -  
ruby185-21.exe. f.stat.size correctly yields 27644627).

---

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [October 16, 2006, 9:01am UTC](https://www.ruby-forum.com/t/strange-read-behaviour-on-windows/72293/2 "2006-10-16T09:01:20Z")

</div>

On 2006.10.16 15:41, Charles L. wrote:

> I switched to the i386-mswin32, but now I getting some more fundamental  
> problems, when doing file reads. I’m typically getting reads back under  
> the requested size, (not a problem), but this is generating premature  
> EOF errors. If I just use File#read, I correctly get the entire file.

Just on an off-chance, is your file open in binary mode?

---

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [October 16, 2006, 11:15am UTC](https://www.ruby-forum.com/t/strange-read-behaviour-on-windows/72293/3 "2006-10-16T11:15:11Z")

</div>

Eero S. wrote:

> On 2006.10.16 15:41, Charles L. wrote:
> 
> > I switched to the i386-mswin32, but now I getting some more fundamental  
> > problems, when doing file reads. I’m typically getting reads back under  
> > the requested size, (not a problem), but this is generating premature  
> > EOF errors. If I just use File#read, I correctly get the entire file.
> 
> Just on an off-chance, is your file open in binary mode?

Oops. Now I feel like an idiot; ‘rb’ should be the default! 🙂 stupid  
windows…

Thank you though, had been banging my head against that one for a while.
