# IO.pos bug?

**URL:** <https://www.ruby-forum.com/t/io-pos-bug/144341>\
**Category:** Ruby\
**Created:** [July 28, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/io-pos-bug/144341 "2008-07-28T20:35:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 28, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/io-pos-bug/144341/1 "2008-07-28T20:35:48Z")

</div>

irb(main):001:0\> f = File.new("/etc/host.conf")  
=\> #\<File:/etc/host.conf\>  
irb(main):002:0\> f.gets  
=\> “multi off\n”  
irb(main):003:0\> f.pos  
=\> 10  
irb(main):004:0\> f.gets  
=\> “order hosts,bind\n”  
irb(main):005:0\> f.pos  
=\> 27  
irb(main):006:0\> f.close  
=\> nil

All right.

irb(main):007:0\> f = File.new("/etc/host.conf")  
=\> #\<File:/etc/host.conf\>  
irb(main):008:0\> f.sysread(5)  
=\> “multi”  
irb(main):009:0\> f.pos  
=\> 5  
irb(main):010:0\> f.sysread(10)  
=\> " off\norder"  
irb(main):011:0\> f.pos  
=\> 5

Bug?  
% ruby1.8 --version  
ruby 1.8.5 (2006-08-25) [i486-linux]

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 28, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/io-pos-bug/144341/2 "2008-07-28T20:39:20Z")

</div>

Hi –

On Tue, 29 Jul 2008, andrey wrote:

> irb(main):006:0\> f.close  
> irb(main):010:0\> f.sysread(10)  
> =\> " off\norder"  
> irb(main):011:0\> f.pos  
> =\> 5
> 
> Bug?  
> % ruby1.8 --version  
> ruby 1.8.5 (2006-08-25) [i486-linux]

It works OK in 1.8.6:

irb(main):001:0\> f = File.new("/etc/hosts")  
=\> #\<File:/etc/hosts\>  
irb(main):002:0\> f.sysread(5)  
=\> "##\n# "  
irb(main):003:0\> f.pos  
=\> 5  
irb(main):004:0\> f.sysread(10)  
=\> “Host Datab”  
irb(main):005:0\> f.pos  
=\> 15

I don’t think 1.8.5 is supported any more, so it might be good to  
update your installation if possible.

David

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 30, 2008, 2:09am UTC](https://www.ruby-forum.com/t/io-pos-bug/144341/3 "2008-07-30T02:09:04Z")

</div>

On Mon, Jul 28, 2008 at 8:38 PM, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > irb(main):004:0\> f.gets  
> > irb(main):008:0\> f.sysread(5)  
> > ruby 1.8.5 (2006-08-25) [i486-linux]
> 
> It works OK in 1.8.6:

[…]

> I don’t think 1.8.5 is supported any more, so it might be good to  
> update your installation if possible.
> 
> David

It works for me with 1.8.5 too:

irb(main):007:0\> f = File.new(“/tmp/jhjh”)  
=\> #\<File:/tmp/jhjh\>  
irb(main):008:0\> f.sysread(5)  
=\> “multi”  
irb(main):009:0\> f.pos  
=\> 5  
irb(main):010:0\> f.sysread(10)  
=\> " off\norder"  
irb(main):011:0\> f.pos  
=\> 15

$ ruby -v  
ruby 1.8.5 (2006-08-25) [powerpc-darwin8.8.0]

I have built this “ruby” myself from source (but note that it is on  
Mac OS X, not Linux).

/Johan H.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 30, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/io-pos-bug/144341/4 "2008-07-30T20:29:17Z")

</div>

Same here…on ruby 1.8.7 (2008-06-20 patchlevel 22) [i686-linux]

Regards, John.

irb(main):001:0\> f = File.new("/etc/hosts")  
=\> #\<File:/etc/hosts\>  
irb(main):002:0\> f.sysread(5)  
=\> “#\n# /”  
irb(main):003:0\> f.pos  
=\> 5  
irb(main):004:0\> f.sysread(10)  
=\> “etc/hosts:”  
irb(main):005:0\> f.pos  
=\> 5  
irb(main):006:0\>

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 31, 2008, 3:33am UTC](https://www.ruby-forum.com/t/io-pos-bug/144341/5 "2008-07-31T03:33:30Z")

</div>

From: John Slobbe [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Same here…on ruby 1.8.7 (2008-06-20 patchlevel 22) [i686-linux]

# irb(main):001:0\> f = File.new(“/etc/hosts”)

# =\> #\<File:/etc/hosts\>

# irb(main):002:0\> f.sysread(5)

# =\> “#\n# /”

# irb(main):003:0\> f.pos

# =\> 5

# irb(main):004:0\> f.sysread(10)

# =\> “etc/hosts:”

# irb(main):005:0\> f.pos

# =\> 5

i seem to remember (don’t know where/when) that you must not mix low  
level calls like #sysread w highlevel calls like #pos, use #read with  
#pos, or use #sysread w #sysseek

but i guess we need a confirmation fr the core dev to verify that. Nobu  
perhaps?

eg,

irb(main):016:0\> File.open “/etc/hosts” do |f|  
irb(main):017:1\* f.sysread 2  
irb(main):018:1\> p f.sysseek(0,IO::SEEK\_CUR)  
irb(main):019:1\> f.sysread 10  
irb(main):020:1\> p f.sysseek(0,IO::SEEK\_CUR)  
irb(main):021:1\> end  
2  
12  
=\> nil

irb(main):022:0\> RUBY\_VERSION  
=\> “1.8.7”

hth.  
kind regards -botp

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 31, 2008, 9:27am UTC](https://www.ruby-forum.com/t/io-pos-bug/144341/6 "2008-07-31T09:27:56Z")

</div>

Hi,

At Thu, 31 Jul 2008 10:31:24 +0900,  
Peña, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in [ruby-talk:309630]:

> i seem to remember (don’t know where/when) that you must not  
> mix low level calls like #sysread w highlevel calls like  
> #pos, use #read with #pos, or use #sysread w #sysseek
> 
> but i guess we need a confirmation fr the core dev to verify  
> that. Nobu perhaps?

You’re correct, it depends on underlying libc, since 1.8 uses  
stdio. 1.9 has its own buffering mechanism and doesn’t have  
that issue.
