# File#file\_descriptor

**URL:** <https://www.ruby-forum.com/t/file-file-descriptor/114373>\
**Category:** Ruby\
**Created:** [September 11, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/file-file-descriptor/114373 "2007-09-11T23:16:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [September 11, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/file-file-descriptor/114373/1 "2007-09-11T23:16:32Z")

</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!

~Wayne

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

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [September 11, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/file-file-descriptor/114373/2 "2007-09-11T23:26:34Z")

</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)  
VALUE io;  
{  
OpenFile \*fptr;  
int fd;

```
 GetOpenFile(io, fptr);
 fd = fileno(fptr->f);
 return INT2FIX(fd);

```

}

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [September 11, 2007, 11:28pm UTC](https://www.ruby-forum.com/t/file-file-descriptor/114373/3 "2007-09-11T23:28:34Z")

</div>

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?

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

```
  Returns an integer representing the numeric file descriptor for
  ios.

     $stdin.fileno #=> 0
     $stdout.fileno #=> 1

  (also known as to_i)
```

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [September 11, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/file-file-descriptor/114373/4 "2007-09-11T23:31:35Z")

</div>

Wayne E. Seguin wrote:

> Wayne E. Seguin  
> Sr. Systems Architect & Systems Administrator

Check the GetOpenFile macro in rubyio.h.

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [September 12, 2007, 3:25am UTC](https://www.ruby-forum.com/t/file-file-descriptor/114373/5 "2007-09-12T03:25:52Z")

</div>

On Sep 11, 2007, at 17:26 , Joel VanderWerf wrote:

> {  
> OpenFile \*fptr;  
> int fd;
> 
> ```
> GetOpenFile(io, fptr);
> fd = fileno(fptr->f);
> return INT2FIX(fd);
> 
> ```
> 
> }

Joel,

Awesome, this is exactly what I was looking for!

Thank you!

~Wayne

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

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [September 12, 2007, 3:31am UTC](https://www.ruby-forum.com/t/file-file-descriptor/114373/6 "2007-09-12T03:31:52Z")

</div>

On Sep 11, 2007, at 17:30 , Tim H. wrote:

> > s///g  
> > Wayne E. Seguin  
> > Sr. Systems Architect & Systems Administrator
> 
> Check the GetOpenFile macro in rubyio.h.

Thank you Tim! Most helpful.

~Wayne

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