# C fwrite equivalent in ruby

**URL:** <https://www.ruby-forum.com/t/c-fwrite-equivalent-in-ruby/149865>\
**Category:** Ruby\
**Created:** [September 30, 2008, 5:12pm UTC](https://www.ruby-forum.com/t/c-fwrite-equivalent-in-ruby/149865 "2008-09-30T17:12:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [September 30, 2008, 5:12pm UTC](https://www.ruby-forum.com/t/c-fwrite-equivalent-in-ruby/149865/1 "2008-09-30T17:12:26Z")

</div>

Hello list

I want to know if there is an equivalent to the C function fwrite in  
ruby.  
Remeber that fwrite takes a buffer and a size of Bytes to write.

the method “write” of the File object takes only the buffer to write as  
a  
parameter, but what I want to do is to write only an amount of bytes  
from  
this buffer, not all the buffer.

Thank you!

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [September 30, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/c-fwrite-equivalent-in-ruby/149865/2 "2008-09-30T17:31:14Z")

</div>

On Sep 30, 2008, at 9:09 AM, Lyes A. wrote:

> this buffer, not all the buffer.
> 
> ## Thank you!
> 
> # =========== | Lyes A. | USTHB, Algiers

io.write buffer[0,len]

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [September 30, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/c-fwrite-equivalent-in-ruby/149865/3 "2008-09-30T18:33:36Z")

</div>

On Sep 30, 11:29 am, “ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> io.write buffer[0,len]

or call io.putc(byte) as many times as necessary.

– Mark.

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [September 30, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/c-fwrite-equivalent-in-ruby/149865/4 "2008-09-30T23:01:24Z")

</div>

On 30.09.2008 17:43, Mark T. wrote:

> On Sep 30, 11:29 am, “ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > io.write buffer[0,len]

One additional remark: String#[] does not copy the internal buffer, so  
you do not have to worry about wasting too much memory.

> or call io.putc(byte) as many times as necessary.

This is likely inefficient.

Kind regards

robert
