# File#file\_descriptor

**URL:** <https://rubytalk.org/t/file-file-descriptor/40551>\
**Category:** ruby-talk\
**Created:** [11 September 2007 21:16 UTC](https://rubytalk.org/t/file-file-descriptor/40551 "2007-09-11T21:16:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wayne\_E\_Seguin](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@Wayne\_E\_Seguin](https://rubytalk.org/u/Wayne_E_Seguin)\
**Post date:** [11 September 2007 21:16 UTC](https://rubytalk.org/t/file-file-descriptor/40551/1 "2007-09-11T21:16:00Z")

</div>

Hopefully someone knows this... I've googled for a while.

When writing a C extension, how does one access the file descriptor of a File object from the C code?

Thanks!

&nbsp;&nbsp;&nbsp;~Wayne

s///g  
Wayne E. Seguin  
Sr. Systems Architect & Systems Administrator

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [11 September 2007 21:26 UTC](https://rubytalk.org/t/file-file-descriptor/40551/2 "2007-09-11T21:26:11Z")

</div>

Wayne E. Seguin wrote:

> Hopefully someone knows this... I've googled for a while.
> 
> When writing a C extension, how does one access the file descriptor of a File object from the C code?

Take a look at this method in io.c:

static VALUE  
rb\_io\_fileno(io)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUE io;  
{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpenFile \*fptr;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int fd;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetOpenFile(io, fptr);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fd = fileno(fptr-\>f);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return INT2FIX(fd);  
}

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [11 September 2007 21:27 UTC](https://rubytalk.org/t/file-file-descriptor/40551/3 "2007-09-11T21:27:04Z")

</div>

$ ri IO#fileno  
-------------------------------------------------------------- IO#fileno  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ios.fileno =\> fixnum  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ios.to\_i =\> fixnum

> **···**
>
> On Sep 11, 2007, at 14:16, Wayne E. Seguin wrote:
> 
> > Hopefully someone knows this... I've googled for a while.
> > 
> > When writing a C extension, how does one access the file descriptor of a File object from the C code?
> 
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns an integer representing the numeric file descriptor for  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ios.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$stdin.fileno #=\> 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$stdout.fileno #=\> 1
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(also known as to\_i)
> 
> --  
> Poor workers blame their tools. Good workers build better tools. The  
> best workers get their tools to do the work for them. -- Syndicate Wars

---

<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:** [11 September 2007 21:30 UTC](https://rubytalk.org/t/file-file-descriptor/40551/4 "2007-09-11T21:30:24Z")

</div>

Wayne E. Seguin wrote:

> Hopefully someone knows this... I've googled for a while.
> 
> When writing a C extension, how does one access the file descriptor of a File object from the C code?
> 
> Thanks!
> 
> &nbsp;&nbsp;~Wayne
> 
> s///g  
> Wayne E. Seguin  
> Sr. Systems Architect & Systems Administrator

Check the GetOpenFile macro in rubyio.h.

> **···**
>
> --  
> 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:** ![Wayne\_E\_Seguin](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@Wayne\_E\_Seguin](https://rubytalk.org/u/Wayne_E_Seguin)\
**Post date:** [12 September 2007 01:25 UTC](https://rubytalk.org/t/file-file-descriptor/40551/5 "2007-09-12T01:25:11Z")

</div>

Joel,

Awesome, this is exactly what I was looking for!

Thank you!

&nbsp;&nbsp;&nbsp;~Wayne

s///g  
Wayne E. Seguin  
Sr. Systems Architect & Systems Administrator

> **···**
>
> On Sep 11, 2007, at 17:26 , Joel VanderWerf wrote:
> 
> > Wayne E. Seguin wrote:
> > 
> > > Hopefully someone knows this... I've googled for a while.  
> > > When writing a C extension, how does one access the file descriptor of a File object from the C code?
> > 
> > Take a look at this method in io.c:
> > 
> > static VALUE  
> > rb\_io\_fileno(io)  
> > &nbsp;&nbsp;&nbsp;&nbsp;VALUE io;  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;OpenFile \*fptr;  
> > &nbsp;&nbsp;&nbsp;&nbsp;int fd;
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;GetOpenFile(io, fptr);  
> > &nbsp;&nbsp;&nbsp;&nbsp;fd = fileno(fptr-\>f);  
> > &nbsp;&nbsp;&nbsp;&nbsp;return INT2FIX(fd);  
> > }

---

<div class="post-metadata">

**Author:** ![Wayne\_E\_Seguin](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@Wayne\_E\_Seguin](https://rubytalk.org/u/Wayne_E_Seguin)\
**Post date:** [12 September 2007 01:30 UTC](https://rubytalk.org/t/file-file-descriptor/40551/6 "2007-09-12T01:30:24Z")

</div>

Thank you Tim! Most helpful.

&nbsp;&nbsp;&nbsp;~Wayne

s///g  
Wayne E. Seguin  
Sr. Systems Architect & Systems Administrator

> **···**
>
> On Sep 11, 2007, at 17:30 , Tim Hunter wrote:
> 
> > Wayne E. Seguin wrote:
> > 
> > > Hopefully someone knows this... I've googled for a while.
> > > 
> > > When writing a C extension, how does one access the file descriptor of a File object from the C code?
> > > 
> > > Thanks!
> > > 
> > > &nbsp;&nbsp;~Wayne
> > > 
> > > s///g  
> > > Wayne E. Seguin  
> > > Sr. Systems Architect & Systems Administrator
> > 
> > Check the GetOpenFile macro in rubyio.h.
