Next: fmemopen, Previous: fgetws, Up: Stdio

### 4.15 `fileno`, `fileno_unlocked`—return file descriptor associated with stream

Synopsis

         #include <stdio.h>
         int fileno(FILE *fp);

         #define _BSD_SOURCE
         #include <stdio.h>
         int fileno_unlocked(FILE *fp);

Description
You can use `fileno` to return the file descriptor identified by fp.

`fileno_unlocked` is a non-thread-safe version of `fileno`. `fileno_unlocked` may only safely be used within a scope protected by flockfile() (or ftrylockfile()) and funlockfile(). This function may safely be used in a multi-threaded program if and only if they are called while the invoking thread owns the (FILE *) object, as is the case after a successful call to the flockfile() or ftrylockfile() functions. If threads are disabled, then `fileno_unlocked` is equivalent to `fileno`.

Returns
`fileno` returns a non-negative integer when successful. If fp is not an open stream, `fileno` returns -1.

Portability
`fileno` is not part of ANSI C. POSIX requires `fileno`.

`fileno_unlocked` is a BSD extension also provided by GNU libc.

Supporting OS subroutines required: none.