| 1 |
'\" t |
| 2 |
.\" Copyright 1990-1991, The Regents of the University of California. |
| 3 |
.\" Copyright 2021, Michael Kerrisk <[email protected]> |
| 4 |
.\" Copyright, the authors of the Linux man-pages project |
| 5 |
.\" |
| 6 |
.\" SPDX-License-Identifier: BSD-4-Clause-UC |
| 7 |
.\" |
| 8 |
.TH fileno 3 2026-02-08 "Linux man-pages 6.18" |
| 9 |
.SH NAME |
| 10 |
fileno \- obtain file descriptor of a stdio stream |
| 11 |
.SH LIBRARY |
| 12 |
Standard C library |
| 13 |
.RI ( libc ,\~ \-lc ) |
| 14 |
.SH SYNOPSIS |
| 15 |
.nf |
| 16 |
.B #include <stdio.h> |
| 17 |
.P |
| 18 |
.BI "int fileno(FILE *" stream ); |
| 19 |
.fi |
| 20 |
.P |
| 21 |
.RS -4 |
| 22 |
Feature Test Macro Requirements for glibc (see |
| 23 |
.BR feature_test_macros (7)): |
| 24 |
.RE |
| 25 |
.P |
| 26 |
.BR fileno (): |
| 27 |
.nf |
| 28 |
_POSIX_C_SOURCE |
| 29 |
.fi |
| 30 |
.SH DESCRIPTION |
| 31 |
The function |
| 32 |
.BR fileno () |
| 33 |
examines the argument |
| 34 |
.I stream |
| 35 |
and returns the integer file descriptor used to implement this stream. |
| 36 |
The file descriptor is still owned by |
| 37 |
.I stream |
| 38 |
and will be closed when |
| 39 |
.BR fclose (3) |
| 40 |
is called. |
| 41 |
Duplicate the file descriptor with |
| 42 |
.BR dup (2) |
| 43 |
before passing it to code that might close it. |
| 44 |
.P |
| 45 |
For the nonlocking counterpart, see |
| 46 |
.BR unlocked_stdio (3). |
| 47 |
.SH RETURN VALUE |
| 48 |
On success, |
| 49 |
.BR fileno () |
| 50 |
returns the file descriptor associated with |
| 51 |
.IR stream . |
| 52 |
On failure, \-1 is returned and |
| 53 |
.I errno |
| 54 |
is set to indicate the error. |
| 55 |
.SH ERRORS |
| 56 |
.TP |
| 57 |
.B EBADF |
| 58 |
.I stream |
| 59 |
is not associated with a file. |
| 60 |
.SH ATTRIBUTES |
| 61 |
For an explanation of the terms used in this section, see |
| 62 |
.BR attributes (7). |
| 63 |
.TS |
| 64 |
allbox; |
| 65 |
lbx lb lb |
| 66 |
l l l. |
| 67 |
Interface Attribute Value |
| 68 |
T{ |
| 69 |
.na |
| 70 |
.nh |
| 71 |
.BR fileno () |
| 72 |
T} Thread safety MT-Safe |
| 73 |
.TE |
| 74 |
.SH STANDARDS |
| 75 |
POSIX.1-2008. |
| 76 |
.SH HISTORY |
| 77 |
POSIX.1-2001. |
| 78 |
.SH SEE ALSO |
| 79 |
.BR open (2), |
| 80 |
.BR fdopen (3), |
| 81 |
.BR stdio (3), |
| 82 |
.BR unlocked_stdio (3) |