获取与流关联的文件描述符。Gets the file descriptor associated with a stream.


int _fileno(
   FILE *stream


指向文件结构的指针。Pointer to the FILE structure.

返回值Return Value

_fileno返回文件描述符。_fileno returns the file descriptor. 无错误返回。There is no error return. 如果stream未指定打开的文件,则结果是不确定的。The result is undefined if stream does not specify an open file. 如果 stream 为NULL_fileno将调用无效参数处理程序,如参数验证中所述。If stream is NULL, _fileno invokes the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,则此函数将返回 -1 并将 errno 设置为 EINVAL。If execution is allowed to continue, this function returns -1 and sets errno to EINVAL.

有关这些及其他错误代码的详细信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrFor more information about these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.


如果stdoutstderr不与输出流(例如,在没有控制台窗口的 Windows 应用程序中)关联,则返回的文件描述符为-2。If stdout or stderr is not associated with an output stream (for example, in a Windows application without a console window), the file descriptor returned is -2. 在早期版本中,返回的文件说明符为 -1。In previous versions, the file descriptor returned was -1. 此更改使应用程序得以将此情况与错误区分开来。This change allows applications to distinguish this condition from an error.


_Fileno例程返回当前与stream关联的文件描述符。The _fileno routine returns the file descriptor currently associated with stream. 此例程作为函数和宏实现。This routine is implemented both as a function and as a macro. 有关选择任一实现的信息,请参阅在函数和宏之间选择For information about choosing either implementation, see Choosing Between Functions and Macros.


函数Function 必需的标头Required header
_fileno_fileno <stdio.h><stdio.h>

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.


// crt_fileno.c
// This program uses _fileno to obtain
// the file descriptor for some standard C streams.

#include <stdio.h>

int main( void )
   printf( "The file descriptor for stdin is %d\n", _fileno( stdin ) );
   printf( "The file descriptor for stdout is %d\n", _fileno( stdout ) );
   printf( "The file descriptor for stderr is %d\n", _fileno( stderr ) );
The file descriptor for stdin is 0
The file descriptor for stdout is 1
The file descriptor for stderr is 2

请参阅See also

流 I/OStream I/O
_fdopen、_wfdopen_fdopen, _wfdopen
_filelength、_filelengthi64_filelength, _filelengthi64
fopen、_wfopen_wfopenfopen, _wfopen
freopen、_wfreopenfreopen, _wfreopen